Android幀緩衝區(Frame Buffer)硬件抽象層(HAL)模塊Gralloc的實現原理分析(11)

         爲了完整性起見,最後咱們再簡要分析函數gralloc_lock和gralloc_unlock的實現,以即可以瞭解一個圖形緩衝區的鎖定和解鎖操做是如何實現的。
        函數gralloc_lock實如今文件hardware/libhardware/modules/gralloc/mapper.cpp文件中,以下所示:
  1. int gralloc_lock(gralloc_module_t const* module,  
  2.         buffer_handle_t handle, int usage,  
  3.         int l, int t, int w, int h,  
  4.         void** vaddr)  
  5. {  
  6.     // this is called when a buffer is being locked for software  
  7.     // access. in thin implementation we have nothing to do since  
  8.     // not synchronization with the h/w is needed.  
  9.     // typically this is used to wait for the h/w to finish with  
  10.     // this buffer if relevant. the data cache may need to be  
  11.     // flushed or invalidated depending on the usage bits and the  
  12.     // hardware.  
  13.   
  14.     if (private_handle_t::validate(handle) < 0)  
  15.         return -EINVAL;  
  16.   
  17.     private_handle_t* hnd = (private_handle_t*)handle;  
  18.     *vaddr = (void*)hnd->base;  
  19.     return 0;  
  20. }  
        從這裏能夠看出,函數gralloc_lock其實並無執行鎖定參數handle所描述的一個緩衝區的操做,它只簡單地將要鎖定的緩衝區的開始地址返回給調用者。
 
        理論上來講,函數gralloc_lock應該檢查參數handle所描述的一個緩衝區是否正在被其進程或者線程使用。若是是的話,那麼函數gralloc_lock就必需要等待,直到要鎖定的緩衝區被其它進程或者線程使用結束爲止,以便接下來能夠獨佔它。因爲函數gralloc_lock實際上並無做這些操做,所以,就必需要由調用者來保證要鎖定的緩衝區當前是沒有被其它進程或者線程使用的。
        函數gralloc_unlock也是實如今文件hardware/libhardware/modules/gralloc/mapper.cpp文件中,以下所示:
  1. int gralloc_unlock(gralloc_module_t const* module,  
  2.         buffer_handle_t handle)  
  3. {  
  4.     // we're done with a software buffer. nothing to do in this  
  5.     // implementation. typically this is used to flush the data cache.  
  6.   
  7.     if (private_handle_t::validate(handle) < 0)  
  8.         return -EINVAL;  
  9.     return 0;  
  10. }  
       函數gralloc_unlock執行的操做原本是恰好與函數gralloc_lock相反的,可是因爲函數gralloc_lock並無真實地鎖定參數handle所描述的一個緩衝區的,所以,函數gralloc_unlock是不須要執行實際的解鎖工做的。
 
       至此,咱們就分析完成Android幀緩衝區硬件抽象層模塊Gralloc的實現原理了。從分析的過程能夠知道,爲了在屏幕中繪製一個指定的畫面,咱們須要:
       1.  分配一個匹配屏幕大小的圖形緩衝區
       2.  將分配好的圖形緩衝區註冊(映射)到當前進程的地址空間來
       3.  將要繪製的畫面的內容寫入到已經註冊好的圖形緩衝區中去,而且渲染(拷貝)到系統幀緩衝區中去
       爲了實現以上三個操做,咱們還須要:
       1. 加載Gralloc模塊
       2. 打開Gralloc模塊中的gralloc設備和fb設備
       其中,gralloc設備負責分配圖形緩衝區,Gralloc模塊負責註冊圖形緩衝區,而fb設備負責渲染圖形緩衝區。
       理解了Gralloc模塊的實現原理以後,就能夠爲後續分析SurfaceFlinger服務的實現打下堅實的基礎了。
相關文章
相關標籤/搜索