Home | History | Annotate | Download | only in HAL3
      1 /* Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
      2  *
      3  * Redistribution and use in source and binary forms, with or without
      4  * modification, are permitted provided that the following conditions are
      5  * met:
      6  *     * Redistributions of source code must retain the above copyright
      7  *       notice, this list of conditions and the following disclaimer.
      8  *     * Redistributions in binary form must reproduce the above
      9  *       copyright notice, this list of conditions and the following
     10  *       disclaimer in the documentation and/or other materials provided
     11  *       with the distribution.
     12  *     * Neither the name of The Linux Foundation nor the names of its
     13  *       contributors may be used to endorse or promote products derived
     14  *       from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  *
     28  */
     29 
     30 #define LOG_TAG "QCamera3StreamMem"
     31 
     32 // System dependencies
     33 #include "gralloc_priv.h"
     34 
     35 // Camera dependencies
     36 #include "QCamera3StreamMem.h"
     37 
     38 using namespace android;
     39 
     40 namespace qcamera {
     41 
     42 /*===========================================================================
     43  * FUNCTION   : QCamera3StreamMem
     44  *
     45  * DESCRIPTION: default constructor of QCamera3StreamMem
     46  *
     47  * PARAMETERS : none
     48  *
     49  * RETURN     : None
     50  *==========================================================================*/
     51 QCamera3StreamMem::QCamera3StreamMem(uint32_t maxHeapBuffer, bool queueHeapBuffers) :
     52         mHeapMem(maxHeapBuffer),
     53         mGrallocMem(maxHeapBuffer),
     54         mMaxHeapBuffers(maxHeapBuffer),
     55         mQueueHeapBuffers(queueHeapBuffers)
     56 {
     57 }
     58 
     59 /*===========================================================================
     60  * FUNCTION   : QCamera3StreamMem
     61  *
     62  * DESCRIPTION: destructor of QCamera3StreamMem
     63  *
     64  * PARAMETERS : none
     65  *
     66  * RETURN     : None
     67  *==========================================================================*/
     68 QCamera3StreamMem::~QCamera3StreamMem()
     69 {
     70     clear();
     71 }
     72 
     73 /*===========================================================================
     74  * FUNCTION   : getCnt
     75  *
     76  * DESCRIPTION: query number of buffers allocated/registered
     77  *
     78  * PARAMETERS : none
     79  *
     80  * RETURN     : number of buffers allocated
     81  *==========================================================================*/
     82 uint32_t QCamera3StreamMem::getCnt()
     83 {
     84     Mutex::Autolock lock(mLock);
     85 
     86     return (mHeapMem.getCnt() + mGrallocMem.getCnt());
     87 }
     88 
     89 /*===========================================================================
     90  * FUNCTION   : getRegFlags
     91  *
     92  * DESCRIPTION: query initial reg flags
     93  *
     94  * PARAMETERS :
     95  *   @regFlags: initial reg flags of the allocated/registered buffers
     96  *
     97  * RETURN     : int32_t type of status
     98  *              NO_ERROR  -- success
     99  *              none-zero failure code
    100  *==========================================================================*/
    101 int QCamera3StreamMem::getRegFlags(uint8_t * regFlags)
    102 {
    103     // Assume that all buffers allocated can be queued.
    104     for (uint32_t i = 0; i < mHeapMem.getCnt(); i ++)
    105         regFlags[i] = (mQueueHeapBuffers ? 1 : 0);
    106     return NO_ERROR;
    107 }
    108 
    109 /*===========================================================================
    110  * FUNCTION   : getFd
    111  *
    112  * DESCRIPTION: return file descriptor of the indexed buffer
    113  *
    114  * PARAMETERS :
    115  *   @index   : index of the buffer
    116  *
    117  * RETURN     : file descriptor
    118  *==========================================================================*/
    119 int QCamera3StreamMem::getFd(uint32_t index)
    120 {
    121     Mutex::Autolock lock(mLock);
    122 
    123     if (index < mMaxHeapBuffers)
    124         return mHeapMem.getFd(index);
    125     else
    126         return mGrallocMem.getFd(index);
    127 }
    128 
    129 /*===========================================================================
    130  * FUNCTION   : getSize
    131  *
    132  * DESCRIPTION: return buffer size of the indexed buffer
    133  *
    134  * PARAMETERS :
    135  *   @index   : index of the buffer
    136  *
    137  * RETURN     : buffer size
    138  *==========================================================================*/
    139 ssize_t QCamera3StreamMem::getSize(uint32_t index)
    140 {
    141     Mutex::Autolock lock(mLock);
    142 
    143     if (index < mMaxHeapBuffers)
    144         return mHeapMem.getSize(index);
    145     else
    146         return mGrallocMem.getSize(index);
    147 }
    148 
    149 /*===========================================================================
    150  * FUNCTION   : invalidateCache
    151  *
    152  * DESCRIPTION: invalidate the cache of the indexed buffer
    153  *
    154  * PARAMETERS :
    155  *   @index   : index of the buffer
    156  *
    157  * RETURN     : int32_t type of status
    158  *              NO_ERROR  -- success
    159  *              none-zero failure code
    160  *==========================================================================*/
    161 int QCamera3StreamMem::invalidateCache(uint32_t index)
    162 {
    163     Mutex::Autolock lock(mLock);
    164 
    165     if (index < mMaxHeapBuffers)
    166         return mHeapMem.invalidateCache(index);
    167     else
    168         return mGrallocMem.invalidateCache(index);
    169 }
    170 
    171 /*===========================================================================
    172  * FUNCTION   : cleanInvalidateCache
    173  *
    174  * DESCRIPTION: clean and invalidate the cache of the indexed buffer
    175  *
    176  * PARAMETERS :
    177  *   @index   : index of the buffer
    178  *
    179  * RETURN     : int32_t type of status
    180  *              NO_ERROR  -- success
    181  *              none-zero failure code
    182  *==========================================================================*/
    183 int QCamera3StreamMem::cleanInvalidateCache(uint32_t index)
    184 {
    185     Mutex::Autolock lock(mLock);
    186 
    187     if (index < mMaxHeapBuffers)
    188         return mHeapMem.cleanInvalidateCache(index);
    189     else
    190         return mGrallocMem.cleanInvalidateCache(index);
    191 }
    192 
    193 /*===========================================================================
    194  * FUNCTION   : cleanCache
    195  *
    196  * DESCRIPTION: clean the cache of the indexed buffer
    197  *
    198  * PARAMETERS :
    199  *   @index   : index of the buffer
    200  *
    201  * RETURN     : int32_t type of status
    202  *              NO_ERROR  -- success
    203  *              none-zero failure code
    204  *==========================================================================*/
    205 int QCamera3StreamMem::cleanCache(uint32_t index)
    206 {
    207     Mutex::Autolock lock(mLock);
    208 
    209     if (index < mMaxHeapBuffers)
    210         return mHeapMem.cleanCache(index);
    211     else
    212         return mGrallocMem.cleanCache(index);
    213 }
    214 
    215 
    216 /*===========================================================================
    217  * FUNCTION   : getBufDef
    218  *
    219  * DESCRIPTION: query detailed buffer information
    220  *
    221  * PARAMETERS :
    222  *   @offset  : [input] frame buffer offset
    223  *   @bufDef  : [output] reference to struct to store buffer definition
    224  *   @index   : [input] index of the buffer
    225  *
    226  * RETURN     : int32_t type of status
    227  *              NO_ERROR  -- success
    228  *              none-zero failure code
    229  *==========================================================================*/
    230 int32_t QCamera3StreamMem::getBufDef(const cam_frame_len_offset_t &offset,
    231         mm_camera_buf_def_t &bufDef, uint32_t index)
    232 {
    233     int32_t ret = NO_ERROR;
    234 
    235     if (index < mMaxHeapBuffers)
    236         ret = mHeapMem.getBufDef(offset, bufDef, index);
    237     else
    238         ret = mGrallocMem.getBufDef(offset, bufDef, index);
    239 
    240     bufDef.mem_info = (void *)this;
    241 
    242     return ret;
    243 }
    244 
    245 /*===========================================================================
    246  * FUNCTION   : getPtr
    247  *
    248  * DESCRIPTION: return virtual address of the indexed buffer
    249  *
    250  * PARAMETERS :
    251  *   @index   : index of the buffer
    252  *
    253  * RETURN     : virtual address
    254  *==========================================================================*/
    255 void* QCamera3StreamMem::getPtr(uint32_t index)
    256 {
    257     Mutex::Autolock lock(mLock);
    258 
    259     if (index < mMaxHeapBuffers)
    260         return mHeapMem.getPtr(index);
    261     else
    262         return mGrallocMem.getPtr(index);
    263 }
    264 
    265 /*===========================================================================
    266  * FUNCTION   : valid
    267  *
    268  * DESCRIPTION: return whether there is a valid buffer at the current index
    269  *
    270  * PARAMETERS :
    271  *   @index   : index of the buffer
    272  *
    273  * RETURN     : true if there is a buffer, false otherwise
    274  *==========================================================================*/
    275 bool QCamera3StreamMem::valid(uint32_t index)
    276 {
    277     Mutex::Autolock lock(mLock);
    278 
    279     if (index < mMaxHeapBuffers)
    280         return (mHeapMem.getSize(index) > 0);
    281     else
    282         return (mGrallocMem.getSize(index) > 0);
    283 }
    284 
    285 /*===========================================================================
    286  * FUNCTION   : registerBuffer
    287  *
    288  * DESCRIPTION: registers frameworks-allocated gralloc buffer_handle_t
    289  *
    290  * PARAMETERS :
    291  *   @buffers : buffer_handle_t pointer
    292  *   @type :    cam_stream_type_t
    293  *
    294  * RETURN     : int32_t type of status
    295  *              NO_ERROR  -- success
    296  *              none-zero failure code
    297  *==========================================================================*/
    298 int QCamera3StreamMem::registerBuffer(buffer_handle_t *buffer,
    299         cam_stream_type_t type)
    300 {
    301     Mutex::Autolock lock(mLock);
    302     return mGrallocMem.registerBuffer(buffer, type);
    303 }
    304 
    305 
    306 /*===========================================================================
    307  * FUNCTION   : unregisterBuffer
    308  *
    309  * DESCRIPTION: unregister buffer
    310  *
    311  * PARAMETERS :
    312  *   @idx     : unregister buffer at index 'idx'
    313  *
    314  * RETURN     : int32_t type of status
    315  *              NO_ERROR  -- success
    316  *              none-zero failure code
    317  *==========================================================================*/
    318 int32_t QCamera3StreamMem::unregisterBuffer(size_t idx)
    319 {
    320     Mutex::Autolock lock(mLock);
    321     return mGrallocMem.unregisterBuffer(idx);
    322 }
    323 
    324 /*===========================================================================
    325  * FUNCTION   : getMatchBufIndex
    326  *
    327  * DESCRIPTION: query buffer index by object ptr
    328  *
    329  * PARAMETERS :
    330  *   @opaque  : opaque ptr
    331  *
    332  * RETURN     : buffer index if match found,
    333  *              -1 if failed
    334  *==========================================================================*/
    335 int QCamera3StreamMem::getMatchBufIndex(void *object)
    336 {
    337     Mutex::Autolock lock(mLock);
    338     return mGrallocMem.getMatchBufIndex(object);
    339 }
    340 
    341 /*===========================================================================
    342  * FUNCTION   : getBufferHandle
    343  *
    344  * DESCRIPTION: return framework pointer
    345  *
    346  * PARAMETERS :
    347  *   @index   : index of the buffer
    348  *
    349  * RETURN     : buffer ptr if match found
    350                 NULL if failed
    351  *==========================================================================*/
    352 void *QCamera3StreamMem::getBufferHandle(uint32_t index)
    353 {
    354     Mutex::Autolock lock(mLock);
    355     return mGrallocMem.getBufferHandle(index);
    356 }
    357 
    358 /*===========================================================================
    359  * FUNCTION   : unregisterBuffers
    360  *
    361  * DESCRIPTION: unregister buffers
    362  *
    363  * PARAMETERS : none
    364  *
    365  * RETURN     : none
    366  *==========================================================================*/
    367 void QCamera3StreamMem::unregisterBuffers()
    368 {
    369     Mutex::Autolock lock(mLock);
    370     mGrallocMem.unregisterBuffers();
    371 }
    372 
    373 
    374 /*===========================================================================
    375  * FUNCTION   : allocate
    376  *
    377  * DESCRIPTION: allocate requested number of buffers of certain size
    378  *
    379  * PARAMETERS :
    380  *   @count   : number of buffers to be allocated
    381  *   @size    : lenght of the buffer to be allocated
    382  *
    383  * RETURN     : int32_t type of status
    384  *              NO_ERROR  -- success
    385  *              none-zero failure code
    386  *==========================================================================*/
    387 int QCamera3StreamMem::allocateAll(size_t size)
    388 {
    389     Mutex::Autolock lock(mLock);
    390     return mHeapMem.allocate(size);
    391 }
    392 
    393 int QCamera3StreamMem::allocateOne(size_t size)
    394 {
    395     Mutex::Autolock lock(mLock);
    396     return mHeapMem.allocateOne(size);
    397 }
    398 
    399 /*===========================================================================
    400  * FUNCTION   : deallocate
    401  *
    402  * DESCRIPTION: deallocate heap buffers
    403  *
    404  * PARAMETERS : none
    405  *
    406  * RETURN     : none
    407  *==========================================================================*/
    408 void QCamera3StreamMem::deallocate()
    409 {
    410     Mutex::Autolock lock(mLock);
    411     mHeapMem.deallocate();
    412 }
    413 
    414 /*===========================================================================
    415  * FUNCTION   : markFrameNumber
    416  *
    417  * DESCRIPTION: We use this function from the request call path to mark the
    418  *              buffers with the frame number they are intended for this info
    419  *              is used later when giving out callback & it is duty of PP to
    420  *              ensure that data for that particular frameNumber/Request is
    421  *              written to this buffer.
    422  * PARAMETERS :
    423  *   @index   : index of the buffer
    424  *   @frame#  : Frame number from the framework
    425  *
    426  * RETURN     : int32_t type of status
    427  *              NO_ERROR  -- success
    428  *              none-zero failure code
    429  *==========================================================================*/
    430 int32_t QCamera3StreamMem::markFrameNumber(uint32_t index, uint32_t frameNumber)
    431 {
    432     Mutex::Autolock lock(mLock);
    433     if (index < mMaxHeapBuffers)
    434         return mHeapMem.markFrameNumber(index, frameNumber);
    435     else
    436         return mGrallocMem.markFrameNumber(index, frameNumber);
    437 }
    438 
    439 /*===========================================================================
    440  * FUNCTION   : getOldestFrameNumber
    441  *
    442  * DESCRIPTION: We use this to fetch the frameNumber expected as per FIFO
    443  *
    444  *
    445  * PARAMETERS :
    446  *   @index   : index of the buffer
    447  *
    448  * RETURN     : int32_t frameNumber
    449  *              positive/zero  -- success
    450  *              negative failure
    451  *==========================================================================*/
    452 int32_t QCamera3StreamMem::getOldestFrameNumber(uint32_t &bufIdx)
    453 {
    454     Mutex::Autolock lock(mLock);
    455     int32_t oldest = INT_MAX;
    456     bool empty = true;
    457     if (mHeapMem.getCnt()){
    458         empty = false;
    459         oldest = mHeapMem.getOldestFrameNumber(bufIdx);
    460     }
    461 
    462     if (mGrallocMem.getCnt()) {
    463         uint32_t grallocBufIdx;
    464         int32_t oldestGrallocFrameNumber = mGrallocMem.getOldestFrameNumber(grallocBufIdx);
    465 
    466         if (empty || (!empty && (oldestGrallocFrameNumber < oldest))){
    467             oldest = oldestGrallocFrameNumber;
    468             bufIdx = grallocBufIdx;
    469         }
    470         empty = false;
    471     }
    472 
    473     if (empty )
    474         return -1;
    475     else
    476         return oldest;
    477 }
    478 
    479 
    480 /*===========================================================================
    481  * FUNCTION   : getFrameNumber
    482  *
    483  * DESCRIPTION: We use this to fetch the frameNumber for the request with which
    484  *              this buffer was given to HAL
    485  *
    486  *
    487  * PARAMETERS :
    488  *   @index   : index of the buffer
    489  *
    490  * RETURN     : int32_t frameNumber
    491  *              positive/zero  -- success
    492  *              negative failure
    493  *==========================================================================*/
    494 int32_t QCamera3StreamMem::getFrameNumber(uint32_t index)
    495 {
    496     Mutex::Autolock lock(mLock);
    497     if (index < mMaxHeapBuffers)
    498         return mHeapMem.getFrameNumber(index);
    499     else
    500         return mGrallocMem.getFrameNumber(index);
    501 }
    502 
    503 /*===========================================================================
    504  * FUNCTION   : getGrallocBufferIndex
    505  *
    506  * DESCRIPTION: We use this to fetch the gralloc buffer index based on frameNumber
    507  *
    508  * PARAMETERS :
    509  *   @frameNumber : frame Number
    510  *
    511  * RETURN     : int32_t buffer index
    512  *              positive/zero  -- success
    513  *              negative failure
    514  *==========================================================================*/
    515 int32_t QCamera3StreamMem::getGrallocBufferIndex(uint32_t frameNumber)
    516 {
    517     Mutex::Autolock lock(mLock);
    518     int32_t index = mGrallocMem.getBufferIndex(frameNumber);
    519     return index;
    520 }
    521 
    522 /*===========================================================================
    523  * FUNCTION   : getHeapBufferIndex
    524  *
    525  * DESCRIPTION: We use this to fetch the heap buffer index based on frameNumber
    526  *
    527  * PARAMETERS :
    528  *   @frameNumber : frame Number
    529  *
    530  * RETURN     : int32_t buffer index
    531  *              positive/zero  -- success
    532  *              negative failure
    533  *==========================================================================*/
    534 int32_t QCamera3StreamMem::getHeapBufferIndex(uint32_t frameNumber)
    535 {
    536     Mutex::Autolock lock(mLock);
    537     int32_t index = mHeapMem.getBufferIndex(frameNumber);
    538     return index;
    539 }
    540 
    541 
    542 /*===========================================================================
    543  * FUNCTION   : getBufferIndex
    544  *
    545  * DESCRIPTION: We use this to fetch the buffer index based on frameNumber
    546  *
    547  * PARAMETERS :
    548  *   @frameNumber : frame Number
    549  *
    550  * RETURN     : int32_t buffer index
    551  *              positive/zero  -- success
    552  *              negative failure
    553  *==========================================================================*/
    554 int32_t QCamera3StreamMem::getBufferIndex(uint32_t frameNumber)
    555 {
    556     Mutex::Autolock lock(mLock);
    557     int32_t index = mGrallocMem.getBufferIndex(frameNumber);
    558 
    559     if (index < 0)
    560         return mHeapMem.getBufferIndex(frameNumber);
    561     else
    562         return index;
    563 }
    564 
    565 
    566 
    567 }; //namespace qcamera
    568