Home | History | Annotate | Download | only in camera
      1 /*
      2  * Copyright (C) Texas Instruments - http://www.ti.com/
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 /**
     18 * @file CameraHalUtilClasses.cpp
     19 *
     20 * This file maps the CameraHardwareInterface to the Camera interfaces on OMAP4 (mainly OMX).
     21 *
     22 */
     23 
     24 #define LOG_TAG "CameraHAL"
     25 
     26 
     27 #include "CameraHal.h"
     28 
     29 namespace android {
     30 
     31 /*--------------------FrameProvider Class STARTS here-----------------------------*/
     32 
     33 int FrameProvider::enableFrameNotification(int32_t frameTypes)
     34 {
     35     LOG_FUNCTION_NAME;
     36     status_t ret = NO_ERROR;
     37 
     38     ///Enable the frame notification to CameraAdapter (which implements FrameNotifier interface)
     39     mFrameNotifier->enableMsgType(frameTypes<<MessageNotifier::FRAME_BIT_FIELD_POSITION
     40                                     , mFrameCallback
     41                                     , NULL
     42                                     , mCookie
     43                                     );
     44 
     45     LOG_FUNCTION_NAME_EXIT;
     46     return ret;
     47 }
     48 
     49 int FrameProvider::disableFrameNotification(int32_t frameTypes)
     50 {
     51     LOG_FUNCTION_NAME;
     52     status_t ret = NO_ERROR;
     53 
     54     mFrameNotifier->disableMsgType(frameTypes<<MessageNotifier::FRAME_BIT_FIELD_POSITION
     55                                     , mCookie
     56                                     );
     57 
     58     LOG_FUNCTION_NAME_EXIT;
     59     return ret;
     60 }
     61 
     62 int FrameProvider::returnFrame(void *frameBuf, CameraFrame::FrameType frameType)
     63 {
     64     status_t ret = NO_ERROR;
     65 
     66     mFrameNotifier->returnFrame(frameBuf, frameType);
     67 
     68     return ret;
     69 }
     70 
     71 void FrameProvider::addFramePointers(void *frameBuf, void *buf)
     72 {
     73   mFrameNotifier->addFramePointers(frameBuf, buf);
     74   return;
     75 }
     76 
     77 void FrameProvider::removeFramePointers()
     78 {
     79   mFrameNotifier->removeFramePointers();
     80   return;
     81 }
     82 
     83 /*--------------------FrameProvider Class ENDS here-----------------------------*/
     84 
     85 /*--------------------EventProvider Class STARTS here-----------------------------*/
     86 
     87 int EventProvider::enableEventNotification(int32_t frameTypes)
     88 {
     89     LOG_FUNCTION_NAME;
     90     status_t ret = NO_ERROR;
     91 
     92     ///Enable the frame notification to CameraAdapter (which implements FrameNotifier interface)
     93     mEventNotifier->enableMsgType(frameTypes<<MessageNotifier::EVENT_BIT_FIELD_POSITION
     94                                     , NULL
     95                                     , mEventCallback
     96                                     , mCookie
     97                                     );
     98 
     99     LOG_FUNCTION_NAME_EXIT;
    100     return ret;
    101 }
    102 
    103 int EventProvider::disableEventNotification(int32_t frameTypes)
    104 {
    105     LOG_FUNCTION_NAME;
    106     status_t ret = NO_ERROR;
    107 
    108     mEventNotifier->disableMsgType(frameTypes<<MessageNotifier::FRAME_BIT_FIELD_POSITION
    109                                     , mCookie
    110                                     );
    111 
    112     LOG_FUNCTION_NAME_EXIT;
    113     return ret;
    114 }
    115 
    116 /*--------------------EventProvider Class ENDS here-----------------------------*/
    117 
    118 /*--------------------CameraArea Class STARTS here-----------------------------*/
    119 
    120 status_t CameraArea::transfrom(size_t width,
    121                                size_t height,
    122                                size_t &top,
    123                                size_t &left,
    124                                size_t &areaWidth,
    125                                size_t &areaHeight)
    126 {
    127     status_t ret = NO_ERROR;
    128     size_t hRange, vRange;
    129     double hScale, vScale;
    130 
    131     LOG_FUNCTION_NAME
    132 
    133     hRange = CameraArea::RIGHT - CameraArea::LEFT;
    134     vRange = CameraArea::BOTTOM - CameraArea::TOP;
    135     hScale = ( double ) width / ( double ) hRange;
    136     vScale = ( double ) height / ( double ) vRange;
    137 
    138     top = ( mTop + vRange / 2 ) * vScale;
    139     left = ( mLeft + hRange / 2 ) * hScale;
    140     areaHeight = ( mBottom + vRange / 2 ) * vScale;
    141     areaHeight -= top;
    142     areaWidth = ( mRight + hRange / 2) * hScale;
    143     areaWidth -= left;
    144 
    145     LOG_FUNCTION_NAME_EXIT
    146 
    147     return ret;
    148 }
    149 
    150 status_t CameraArea::checkArea(ssize_t top,
    151                                ssize_t left,
    152                                ssize_t bottom,
    153                                ssize_t right,
    154                                ssize_t weight)
    155 {
    156 
    157     //Handles the invalid regin corner case.
    158     if ( ( 0 == top ) && ( 0 == left ) && ( 0 == bottom ) && ( 0 == right ) && ( 0 == weight ) ) {
    159         return NO_ERROR;
    160     }
    161 
    162     if ( ( CameraArea::WEIGHT_MIN > weight ) ||  ( CameraArea::WEIGHT_MAX < weight ) ) {
    163         CAMHAL_LOGEB("Camera area weight is invalid %d", weight);
    164         return -EINVAL;
    165     }
    166 
    167     if ( ( CameraArea::TOP > top ) || ( CameraArea::BOTTOM < top ) ) {
    168         CAMHAL_LOGEB("Camera area top coordinate is invalid %d", top );
    169         return -EINVAL;
    170     }
    171 
    172     if ( ( CameraArea::TOP > bottom ) || ( CameraArea::BOTTOM < bottom ) ) {
    173         CAMHAL_LOGEB("Camera area bottom coordinate is invalid %d", bottom );
    174         return -EINVAL;
    175     }
    176 
    177     if ( ( CameraArea::LEFT > left ) || ( CameraArea::RIGHT < left ) ) {
    178         CAMHAL_LOGEB("Camera area left coordinate is invalid %d", left );
    179         return -EINVAL;
    180     }
    181 
    182     if ( ( CameraArea::LEFT > right ) || ( CameraArea::RIGHT < right ) ) {
    183         CAMHAL_LOGEB("Camera area right coordinate is invalid %d", right );
    184         return -EINVAL;
    185     }
    186 
    187     if ( left >= right ) {
    188         CAMHAL_LOGEA("Camera area left larger than right");
    189         return -EINVAL;
    190     }
    191 
    192     if ( top >= bottom ) {
    193         CAMHAL_LOGEA("Camera area top larger than bottom");
    194         return -EINVAL;
    195     }
    196 
    197     return NO_ERROR;
    198 }
    199 
    200 status_t CameraArea::parseAreas(const char *area,
    201                                 size_t areaLength,
    202                                 Vector< sp<CameraArea> > &areas)
    203 {
    204     status_t ret = NO_ERROR;
    205     char *ctx;
    206     char *pArea = NULL;
    207     char *pStart = NULL;
    208     char *pEnd = NULL;
    209     const char *startToken = "(";
    210     const char endToken = ')';
    211     const char sep = ',';
    212     ssize_t top, left, bottom, right, weight;
    213     char *tmpBuffer = NULL;
    214     sp<CameraArea> currentArea;
    215 
    216     LOG_FUNCTION_NAME
    217 
    218     if ( ( NULL == area ) ||
    219          ( 0 >= areaLength ) )
    220         {
    221         return -EINVAL;
    222         }
    223 
    224     tmpBuffer = ( char * ) malloc(areaLength);
    225     if ( NULL == tmpBuffer )
    226         {
    227         return -ENOMEM;
    228         }
    229 
    230     memcpy(tmpBuffer, area, areaLength);
    231 
    232     pArea = strtok_r(tmpBuffer, startToken, &ctx);
    233 
    234     do
    235         {
    236 
    237         pStart = pArea;
    238         if ( NULL == pStart )
    239             {
    240             CAMHAL_LOGEA("Parsing of the left area coordinate failed!");
    241             ret = -EINVAL;
    242             break;
    243             }
    244         else
    245             {
    246             left = static_cast<ssize_t>(strtol(pStart, &pEnd, 10));
    247             }
    248 
    249         if ( sep != *pEnd )
    250             {
    251             CAMHAL_LOGEA("Parsing of the top area coordinate failed!");
    252             ret = -EINVAL;
    253             break;
    254             }
    255         else
    256             {
    257             top = static_cast<ssize_t>(strtol(pEnd+1, &pEnd, 10));
    258             }
    259 
    260         if ( sep != *pEnd )
    261             {
    262             CAMHAL_LOGEA("Parsing of the right area coordinate failed!");
    263             ret = -EINVAL;
    264             break;
    265             }
    266         else
    267             {
    268             right = static_cast<ssize_t>(strtol(pEnd+1, &pEnd, 10));
    269             }
    270 
    271         if ( sep != *pEnd )
    272             {
    273             CAMHAL_LOGEA("Parsing of the bottom area coordinate failed!");
    274             ret = -EINVAL;
    275             break;
    276             }
    277         else
    278             {
    279             bottom = static_cast<ssize_t>(strtol(pEnd+1, &pEnd, 10));
    280             }
    281 
    282         if ( sep != *pEnd )
    283             {
    284             CAMHAL_LOGEA("Parsing of the weight area coordinate failed!");
    285             ret = -EINVAL;
    286             break;
    287             }
    288         else
    289             {
    290             weight = static_cast<ssize_t>(strtol(pEnd+1, &pEnd, 10));
    291             }
    292 
    293         if ( endToken != *pEnd )
    294             {
    295             CAMHAL_LOGEA("Malformed area!");
    296             ret = -EINVAL;
    297             break;
    298             }
    299 
    300         ret = checkArea(top, left, bottom, right, weight);
    301         if ( NO_ERROR != ret ) {
    302             break;
    303         }
    304 
    305         currentArea = new CameraArea(top, left, bottom, right, weight);
    306         CAMHAL_LOGDB("Area parsed [%dx%d, %dx%d] %d",
    307                      ( int ) top,
    308                      ( int ) left,
    309                      ( int ) bottom,
    310                      ( int ) right,
    311                      ( int ) weight);
    312         if ( NULL != currentArea.get() )
    313             {
    314             areas.add(currentArea);
    315             }
    316         else
    317             {
    318             ret = -ENOMEM;
    319             break;
    320             }
    321 
    322         pArea = strtok_r(NULL, startToken, &ctx);
    323 
    324         }
    325     while ( NULL != pArea );
    326 
    327     if ( NULL != tmpBuffer )
    328         {
    329         free(tmpBuffer);
    330         }
    331 
    332     LOG_FUNCTION_NAME_EXIT
    333 
    334     return ret;
    335 }
    336 
    337 bool CameraArea::areAreasDifferent(Vector< sp<CameraArea> > &area1,
    338                                     Vector< sp<CameraArea> > &area2) {
    339     if (area1.size() != area2.size()) {
    340         return true;
    341     }
    342 
    343     // not going to care about sorting order for now
    344     for (int i = 0; i < area1.size(); i++) {
    345         if (!area1.itemAt(i)->compare(area2.itemAt(i))) {
    346             return true;
    347         }
    348     }
    349 
    350     return false;
    351 }
    352 
    353 bool CameraArea::compare(const sp<CameraArea> &area) {
    354     return ((mTop == area->mTop) && (mLeft == area->mLeft) &&
    355             (mBottom == area->mBottom) && (mRight == area->mRight) &&
    356             (mWeight == area->mWeight));
    357 }
    358 
    359 
    360 /*--------------------CameraArea Class ENDS here-----------------------------*/
    361 
    362 };
    363