In the previous article, Gralloc, the hardware abstraction layer of Android graphic display, introduced the definition of the Gralloc module, the opening process of the fb device in the Gralloc module, and the opening process of the gpu device. The Gralloc module loading process has been described in detail in the source code analysis of the Android Hardware abstraction Hardware Library Loading Process. The Gralloc Hardware abstraction layer is introduced through Android graphic display, I believe that you have a rough understanding of the design of the Gralloc module. This article continues to analyze the graphic display content of the Android system based on the previous article. We know that the Gralloc module defines a gpu device to allocate a graphic buffer. in the previous article, we have introduced the gpu device opening process. When you open a gpu device, this article describes how to allocate and release a graphic buffer. Android defines a tool class GraphicBufferAllocator for the upper layer. This class is used to access the gpu device alloc_device_t in the Gralloc Hardware Abstraction module and is responsible for allocating and recycling the graphic buffer.
GraphicBufferAllocator: GraphicBufferAllocator (): mAllocDev (0) {hw_module_t const * module; // obtain the first address of the module descriptor int err = hw_get_module (struct, & module) based on the module ID ); ALOGE_IF (err, "FATAL: can't find the % s module", GRALLOC_HARDWARE_MODULE_ID); if (err = 0) {// call the open method of the hardware abstraction layer, create an hw_device_t object and register hw_module_t to gralloc_open (module, & mAllocDev);} In hw_device_t );}}The hw_get_module function is used to load the Hardware Abstraction module. For details, refer to the source code analysis of the Android Hardware abstraction Hardware library loading process. The gralloc_open function is used to open the gpu device. It has been described in details in Gralloc, the hardware abstraction layer for Android graphics display.
static inline int gralloc_open(const struct hw_module_t* module, struct alloc_device_t** device) {//gralloc_device_open return module->methods->open(module, GRALLOC_HARDWARE_GPU0, (struct hw_device_t**)device);}Hw_module_t is used to describe the hardware abstraction layer module, and hw_device_t is used to describe the hardware device
Int gralloc_device_open (const hw_module_t * module, const char * name, hw_device_t ** device) {int status =-EINVAL; // open the module named gpu0 if (! Strcmp (name, GRALLOC_HARDWARE_GPU0) {gralloc_context_t * dev; dev = (gralloc_context_t *) malloc (sizeof (* dev);/* initialize our state here */memset (dev, 0, sizeof (* dev);/* initialize the procs */dev-> device. common. tag = HARDWARE_DEVICE_TAG; dev-> device. common. version = 0; dev-> device. common. module = const_cast Open the gpu device in the Gralloc module to obtain the data structure gralloc_context_t describing the gpu device, and save it in the mAllocDev member variable of the GraphicBufferAllocator class. The mAllocDev type is alloc_device_t, because the type of the device in the first field variable of gralloc_context_t is alloc_device_t, the pointer to the first address of gralloc_context_t also points to the first address of alloc_device_t. The Inheritance Method in C language is used here.
The previous section introduced the gpu devices in the Gralloc module used to allocate a graphic buffer. After GraphicBufferAllocator enables the gpu device, it allocates the graphic buffer through the gpu device. Therefore, GraphicBufferAllocator provides interfaces for accessing gpu devices on the upper layer.
GraphicBufferAllocator: alloc () --> mAllocDev-> alloc () --> alloc_device_t-> alloc () --> gralloc_alloc () allocates a graphic Buffer
GraphicBufferAllocator: free () --> mAllocDev-> free () --> alloc_device_t-> free () --> gralloc_free () release the graphic Buffer
Graph Buffer Allocation Process
Status_t GraphicBufferAllocator: alloc (uint32_t w, uint32_t h, PixelFormat format, int usage, buffer_handle_t * handle, int32_t * stride) {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. if (! W |! H) w = h = 1; status_t err; // call the alloc function in alloc_device_t to allocate the image buffer err = mAllocDev-> alloc (mAllocDev, w, h, format, usage, handle, stride); // The graphic buffer is allocated successfully. The buffer_handle_t in the graphic buffer is saved as a key-value pair to the member variable sAllocList table, this interface is used to dump all the graphic buffer Information allocated by the system. If (err = NO_ERROR) {Mutex: Autolock _ l (sLock); KeyedVector <buffer_handle_t, alloc_rec_t> & list (sAllocList); int bpp = bytepixel (format ); if (bpp <0) {// probably a HAL custom format. in any case, we don't know // what its pixel size is. bpp = 0;} alloc_rec_t rec; rec. w = w; rec. h = h; rec. s = * stride; rec. format = format; rec. usage = usage; rec. size = h * stride [0] * bpp; list. add (* handle, rec);} return err ;}You can call the alloc function in alloc_device_t to allocate a graphic buffer. When you open a gpu device, you have registered the graphic Buffer Allocation Function for alloc_device_t: dev-> device. alloc = gralloc_alloc; therefore, the gralloc_alloc function will be called.
Hardware \ libhardware \ modules \ gralloc. cpp
Static int gralloc_alloc (alloc_device_t * dev, // gpu device descriptor int w, // Image Width int h, // Image Height int format, // graphic format int usage, // graph buffer usage type buffer_handle_t * pHandle, // graph buffer int * pStride to be allocated) // The number of pixels contained in a row in the allocated graph buffer {if (! PHandle |! PStride) return-EINVAL; size_t size, stride; int align = 4; int bpp = 0; switch (format) {case HAL_PIXEL_FORMAT_RGBA_8888: case HAL_PIXEL_FORMAT_RGBX_8888: case when: // a pixel must be expressed in 32 bits, that is, 4 bpp = 4; break; case HAL_PIXEL_FORMAT_RGB_888: // a pixel must be described in 24 bits, that is, 3 bytes bpp = 3; break; case HAL_PIXEL_FORMAT_RGB_565: case HAL_PIXEL_FORMAT_RGBA_5551: case HAL_PIXEL_FORMAT_RGBA_4444: // required for an image Use 16 bits to describe, that is, two bytes bpp = 2; break; default: return-EINVAL;} // w indicates the width of the image saved in the graphic buffer to be allocated, w * bpp can obtain the number of bytes required to save a row of pixels and align them to the four-byte boundary size_t bpr = (w * bpp + (align-1 ))&~ (Align-1); // h indicates the height of the image stored in the graphic buffer to be allocated, the number of bytes required to save the entire image can be obtained by using the bpr * h size = bpr * h; // The number of pixels in a row in the graphic buffer to be allocated stride = bpr/bpp; int err; // The number of pixels contained in a row in the graphic buffer to be allocated. if (usage & GRALLOC_USAGE_HW_FB) {// The graphic buffer err = gralloc_alloc_framebuffer (dev, size, usage, pHandle);} else {// allocate the graphic buffer err = gralloc_alloc_buffer (dev, size, usage, pHandle) from the memory;} if (err <0) {return err;} * pStride = stride; return 0 ;}Different allocation methods are selected based on the purpose of the graphic buffer allocated by the request. The graphic buffer can be allocated from the system frame buffer or directly from the memory, when the purpose flag of the graphic buffer requested for allocation is set to GRALLOC_USAGE_HW_FB, this graphic buffer is allocated from the FrameBuffer system frame buffer.
System Frame Buffer Allocation Buffer
The gralloc_alloc_framebuffer function is used to allocate a graphic buffer from FrameBuffer.
static int gralloc_alloc_framebuffer(alloc_device_t* dev, size_t size, int usage, buffer_handle_t* pHandle){ private_module_t* m = reinterpret_cast<private_module_t*>(dev->common.module); pthread_mutex_lock(&m->lock); int err = gralloc_alloc_framebuffer_locked(dev, size, usage, pHandle); pthread_mutex_unlock(&m->lock); return err;}To ensure thread security during the allocation process, lock in private_module_t is used to synchronize multithreading.
Static int gralloc_alloc_framebuffer_locked (alloc_device_t * dev, size_t size, int usage, buffer_handle_t * pHandle) {// find the system frame buffer description body private_module_t * m = reinterpret_cast <private_module_t *> (dev-> common. module);/*** before allocating a graphic buffer from the system frame buffer, initialize the system frame buffer. The mapFrameBufferLocked function is used to initialize the system frame buffer. During the initialization of the system frame buffer, private_ha * ndle_t is used to describe the information of the entire system frame buffer and kept in the member framebuffer of private_module_t, if the member variable framebuffer is empty, the system frame buffer is also initialized. **/If (m-> framebuffer = NULL) {// The system frame buffer has not been initialized. // The system frame buffer has not been initialized, ing to the virtual address space of the current process to int err = mapFrameBufferLocked (m); if (err <0) {return err ;}} // obtain the usage of the system frame buffer. const uint32_t bufferMask = m-> bufferMask; // how many graphic buffers can the system frame buffer be divided into to use const uint32_t numBuffers = m-> numBuffers; // The size of memory occupied by the content on the display screen of the device. const size_t bufferSize = m-> finfo. line_length * m-> info. yres; // if the system frame buffer has only one image buffer size, that is, the value of the variable numBuffers is equal to 1, this graphic buffer always uses Use if (numBuffers = 1) as the system's primary image buffer {// The image buffer cannot be allocated in the system frame buffer, allocate graph buffer int newUsage = (usage &~ GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D; return gralloc_alloc_buffer (dev, bufferSize, newUsage, pHandle );} // all the graphic buffers in the system frame buffer are allocated if (bufferMask >=( (1LU <numBuffers)-1) {return-ENOMEM ;} // base address pointing to the system frame buffer intptr_t vaddr = intptr_t (m-> framebuffer-> base ); // create a private_handle_t struct hnd to describe the graph buffer private_handle_t * hnd = new private_handle_t (dup (m-> framebuffer-> fd), size, private_handle_t :: PRIV_FLAGS_FRAMEBUFFER); // find the idle graph buffer from bufferMask (uint32_t I = 0; I <numBuffers; I ++) {if (bufferMask & (1LU <I) = 0) {m-> bufferMask | = (1LU <I); break ;} // The size of the graphic buffer allocated from the system frame buffer is exactly the same as the vaddr + = bufferSize of the content on the screen ;} // The starting address of the allocated graph buffer is stored in the member variable base of the created private_handle_t struct hnd. hnd-> base = vaddr; // The offset between the starting address of the allocated graphic buffer and the base address of the system frame buffer. hnd-> offset = vaddr-intptr_t (m-> framebuffer-> base ); * pHandle = hnd; return 0 ;}
Buffer Allocation in memory
Static int gralloc_alloc_buffer (alloc_device_t * dev, size_t size, int usage, buffer_handle_t * pHandle) {int err = 0; int fd =-1; // The allocated graph buffer size page alignment size = roundUpToPageSize (size); // create an anonymous shared memory area fd = ashmem_create_region ("gralloc-buffer", size) with the specified size ); if (fd <0) {ALOGE ("couldn't create ashmem (% s)", strerror (-errno); err =-errno ;} if (err = 0) {// create a private_handle_t struct hnd to describe the graph buffer to be allocated private_handle_t * hnd = new private_handle_t (fd, size, 0 ); gralloc_module_t * module = reinterpret_cast <gralloc_module_t *> (dev-> common. module); // map the allocated graphic buffer err = mapBuffer (module, hnd); if (err = 0) {* pHandle = hnd ;}} ALOGE_IF (err, "gralloc failed err = % s", strerror (-err); return err ;}This function first creates an anonymous shared memory named "gralloc-buffer" and constructs a private_handle_t object based on the information of the anonymous shared memory. This object is used to describe the allocated graphic buffer zone, then, the created anonymous shared memory is mapped to the virtual address space of the current process. The address space ing is not executed when the system frame buffer is allocated to the graphic buffer, but the ing process needs to be executed here. Why? This is because when the mapFrameBufferLocked function is executed to initialize the system frame buffer, the entire frame buffer of the system has been mapped to the current process address space, the allocation of buffer in the system frame buffer does not require repeated ing, but the allocation of buffer in the memory is different. The buffer allocated in the memory is an anonymous shared memory, the anonymous shared memory is not mapped to the address space of the current process. Therefore, you need to complete the ing process to prepare the application process to directly access this buffer.
int mapBuffer(gralloc_module_t const* module,private_handle_t* hnd){ void* vaddr; return gralloc_map(module, hnd, &vaddr);}Gralloc_map maps a graphic buffer described by the hnd parameter to the address space of the current process.
Static int gralloc_map (gralloc_module_t const * module, buffer_handle_t handle, void ** vaddr) {private_handle_t * hnd = (private_handle_t *) handle; // if the current buffer is not allocated from the system frame buffer if (! (Hnd-> flags & private_handle_t: PRIV_FLAGS_FRAMEBUFFER) {size_t size = hnd-> size; void * mappedAddress = mmap (0, size, PROT_READ | PROT_WRITE, MAP_SHARED, hnd-> fd, 0); if (mappedAddress = MAP_FAILED) {ALOGE ("cocould not mmap % s", strerror (errno); return-errno ;} hnd-> base = intptr_t (mappedAddress) + hnd-> offset;} * vaddr = (void *) hnd-> base; return 0 ;}When initializing the system frame buffer, the system frame buffer has been mapped to the process address space. Therefore, if you find that the graphic buffer to be registered is allocated in the system frame buffer, therefore, you do not need to map the graphic buffer.
Graphic buffer release process
Status_t GraphicBufferAllocator: free (handle) {ATRACE_CALL (); status_t err; // call the free function in alloc_device_t to release the image buffer err = mAllocDev-> free (mAllocDev, handle ); ALOGW_IF (err, "free (...) failed % d (% s) ", err, strerror (-err); if (err = NO_ERROR) {Mutex: Autolock _ l (sLock); KeyedVector <buffer_handle_t, alloc_rec_t> & list (sAllocList); list. removeItem (handle);} return err ;}GraphicBufferAllocator class release graphics buffer
Static int gralloc_free (alloc_device_t * dev, buffer_handle_t handle) {if (private_handle_t: validate (handle) <0) return-EINVAL; private_handle_t const * hnd = reinterpret_cast <private_handle_t const *> (handle); // if the currently released buffer is allocated from the system frame buffer if (hnd-> flags & private_handle_t :: PRIV_FLAGS_FRAMEBUFFER) {// alloc_device_t --> hw_device_t --> hw_module_t --> private_module_t * m = reinterpret _ Cast <private_module_t *> (dev-> common. module); // calculate the memory size occupied by the content of a screen on the display. const size_t bufferSize = m-> finfo. line_length * m-> info. yres; // calculate the index of the currently released buffer in the system frame buffer, the system frame buffer is divided into blocks of bufferSize int index = (hnd-> base-m-> framebuffer-> base)/bufferSize; m-> bufferMask & = ~ (1 <index );} else {// The currently released buffer is allocated from the system memory // alloc_device_t --> hw_device_t --> hw_module_t --> gralloc_module_t * module = reinterpret_cast <constraint *> (dev-> common. module); // terminateBuffer (module, const_cast <private_handle_t *> (hnd);} close (hnd-> fd); delete hnd; return 0 ;}This function also processes the buffer allocated in the system frame buffer and the buffer allocated in the memory respectively. For the buffer release process allocated in the system frame buffer, it only clears the pointer in the bufferMask. For the buffer allocated in the memory, the terminateBuffer function is called to release the buffer.
int terminateBuffer(gralloc_module_t const* module,private_handle_t* hnd){ if (hnd->base) { // this buffer was mapped, unmap it now gralloc_unmap(module, hnd); } return 0;}This function calls gralloc_unmap to cancel the buffer ing to the current process address space.
static int gralloc_unmap(gralloc_module_t const* module,buffer_handle_t handle){ private_handle_t* hnd = (private_handle_t*)handle; if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) { void* base = (void*)hnd->base; size_t size = hnd->size; if (munmap(base, size) < 0) { ALOGE("Could not unmap %s", strerror(errno)); } } hnd->base = 0; return 0;}For the release of the graphic buffer allocated in the memory, the first step is to cancel the ing of the graphic buffer from the current process address space before calling close (fd) to release this buffer, because the buffer allocated in the memory is an anonymous shared memory, when close (fd) is called, the anonymous shared memory driver ashmem_release is automatically called, this function is used to reclaim the created anonymous shared memory. For the buffer allocated from the system frame buffer, you only need to clear the pointer in bufferMask.