OMXFocus.cpp revision 5458bdc45048501d1919b14d22456de91f7e8950
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/**
19* @file OMXFocus.cpp
20*
21* This file contains functionality for handling focus configurations.
22*
23*/
24
25#undef LOG_TAG
26
27#define LOG_TAG "CameraHAL"
28
29#include "CameraHal.h"
30#include "OMXCameraAdapter.h"
31#include "ErrorUtils.h"
32
33#define TOUCH_FOCUS_RANGE 0xFF
34#define AF_CALLBACK_TIMEOUT 5000000 //5 seconds timeout
35
36namespace android {
37
38status_t OMXCameraAdapter::setParametersFocus(const CameraParameters &params,
39                                              BaseCameraAdapter::AdapterState state)
40{
41    status_t ret = NO_ERROR;
42    const char *str = NULL;
43    Vector< sp<CameraArea> > tempAreas;
44    size_t MAX_FOCUS_AREAS;
45
46    LOG_FUNCTION_NAME;
47
48    Mutex::Autolock lock(mFocusAreasLock);
49
50    str = params.get(CameraParameters::KEY_FOCUS_AREAS);
51
52    MAX_FOCUS_AREAS = atoi(params.get(CameraParameters::KEY_MAX_NUM_FOCUS_AREAS));
53
54    if ( NULL != str ) {
55        ret = CameraArea::parseAreas(str, ( strlen(str) + 1 ), tempAreas);
56    }
57
58    if ( (NO_ERROR == ret) && CameraArea::areAreasDifferent(mFocusAreas, tempAreas) ) {
59        mFocusAreas.clear();
60        mFocusAreas = tempAreas;
61        if ( MAX_FOCUS_AREAS < mFocusAreas.size() ) {
62            CAMHAL_LOGEB("Focus areas supported %d, focus areas set %d",
63                         MAX_FOCUS_AREAS,
64                         mFocusAreas.size());
65            ret = -EINVAL;
66        }
67        else {
68            if ( !mFocusAreas.isEmpty() ) {
69                setTouchFocus();
70            }
71        }
72    }
73
74    LOG_FUNCTION_NAME;
75
76    return ret;
77}
78
79status_t OMXCameraAdapter::doAutoFocus()
80{
81    status_t ret = NO_ERROR;
82    OMX_ERRORTYPE eError = OMX_ErrorNone;
83    OMX_IMAGE_CONFIG_FOCUSCONTROLTYPE focusControl;
84    OMX_PARAM_FOCUSSTATUSTYPE focusStatus;
85
86    LOG_FUNCTION_NAME;
87
88    if ( OMX_StateExecuting != mComponentState )
89        {
90        CAMHAL_LOGEA("OMX component not in executing state");
91        returnFocusStatus(false);
92        return NO_INIT;
93        }
94
95    if ( 0 != mDoAFSem.Count() )
96        {
97        CAMHAL_LOGEB("Error mDoAFSem semaphore count %d", mDoAFSem.Count());
98        return NO_INIT;
99        }
100
101    // If the app calls autoFocus, the camera will stop sending face callbacks.
102    pauseFaceDetection(true);
103
104    // This is needed for applying FOCUS_REGION correctly
105    if ( (!mFocusAreas.isEmpty()) && (!mFocusAreas.itemAt(0)->isZeroArea()))
106    {
107    //Disable face priority
108    setAlgoPriority(FACE_PRIORITY, FOCUS_ALGO, false);
109
110    //Enable region algorithm priority
111    setAlgoPriority(REGION_PRIORITY, FOCUS_ALGO, true);
112    }
113
114    OMX_INIT_STRUCT_PTR (&focusControl, OMX_IMAGE_CONFIG_FOCUSCONTROLTYPE);
115    focusControl.eFocusControl = ( OMX_IMAGE_FOCUSCONTROLTYPE ) mParameters3A.Focus;
116
117    if (mParameters3A.FocusLock) {
118        // this basically means user never called cancelAutoFocus after a scan...
119        // if this is the case we need to unlock AF to ensure we will do a scan
120        if (set3ALock(mUserSetExpLock, mUserSetWbLock, OMX_FALSE) != NO_ERROR) {
121            CAMHAL_LOGEA("Error Unlocking 3A locks");
122        } else {
123            CAMHAL_LOGDA("AE/AWB unlocked successfully");
124        }
125
126    } else if ( mParameters3A.Focus == OMX_IMAGE_FocusControlAuto ) {
127        // In case we have CAF running we should first check the AF status.
128        // If it has managed to lock, then do as usual and return status
129        // immediately. If lock is not available, then switch temporarily
130        // to 'autolock' and do normal AF.
131        ret = checkFocus(&focusStatus);
132        if ( NO_ERROR != ret ) {
133            CAMHAL_LOGEB("Focus status check failed 0x%x!", ret);
134            return ret;
135        } else {
136            CAMHAL_LOGDB("Focus status check 0x%x!", focusStatus.eFocusStatus);
137        }
138    }
139
140    if ( ( focusControl.eFocusControl != OMX_IMAGE_FocusControlAuto ) &&
141         ( focusControl.eFocusControl != ( OMX_IMAGE_FOCUSCONTROLTYPE )
142                 OMX_IMAGE_FocusControlAutoInfinity ) ) {
143
144        ret = RegisterForEvent(mCameraAdapterParameters.mHandleComp,
145                                    (OMX_EVENTTYPE) OMX_EventIndexSettingChanged,
146                                    OMX_ALL,
147                                    OMX_IndexConfigCommonFocusStatus,
148                                    mDoAFSem);
149
150        if ( NO_ERROR == ret ) {
151            ret = setFocusCallback(true);
152        }
153
154        eError =  OMX_SetConfig(mCameraAdapterParameters.mHandleComp,
155                                OMX_IndexConfigFocusControl,
156                                &focusControl);
157
158        if ( OMX_ErrorNone != eError ) {
159            CAMHAL_LOGEB("Error while starting focus 0x%x", eError);
160            return INVALID_OPERATION;
161        } else {
162            CAMHAL_LOGDA("Autofocus started successfully");
163        }
164
165       if(mDoAFSem.WaitTimeout(AF_CALLBACK_TIMEOUT) != NO_ERROR) {
166            //If somethiing bad happened while we wait
167            if (mComponentState == OMX_StateInvalid) {
168                CAMHAL_LOGEA("Invalid State after Auto Focus Exitting!!!");
169                return EINVAL;
170            }
171
172            //Disable auto focus callback from Ducati
173            setFocusCallback(false);
174            CAMHAL_LOGEA("Autofocus callback timeout expired");
175            RemoveEvent(mCameraAdapterParameters.mHandleComp,
176                                        (OMX_EVENTTYPE) OMX_EventIndexSettingChanged,
177                                        OMX_ALL,
178                                        OMX_IndexConfigCommonFocusStatus,
179                                        NULL );
180            returnFocusStatus(true);
181        } else {
182            CAMHAL_LOGDA("Autofocus callback received");
183            //Disable auto focus callback from Ducati
184            setFocusCallback(false);
185            ret = returnFocusStatus(false);
186        }
187    } else { // Focus mode in continuous
188        if ( NO_ERROR == ret ) {
189            ret = returnFocusStatus(false);
190            mPending3Asettings |= SetFocus;
191        }
192    }
193
194    LOG_FUNCTION_NAME_EXIT;
195
196    return ret;
197}
198
199status_t OMXCameraAdapter::stopAutoFocus()
200{
201    status_t ret = NO_ERROR;
202    OMX_ERRORTYPE eError = OMX_ErrorNone;
203    OMX_IMAGE_CONFIG_FOCUSCONTROLTYPE focusControl;
204
205    LOG_FUNCTION_NAME;
206
207    if ( OMX_StateExecuting != mComponentState )
208        {
209        CAMHAL_LOGEA("OMX component not in executing state");
210        return NO_INIT;
211        }
212
213    if ( mParameters3A.Focus == OMX_IMAGE_FocusControlAutoInfinity ) {
214        // No need to stop focus if we are in infinity mode. Nothing to stop.
215        return NO_ERROR;
216    }
217
218    if ( NO_ERROR == ret )
219       {
220       //Disable the callback first
221       ret = setFocusCallback(false);
222       }
223
224    if ( NO_ERROR == ret )
225        {
226        OMX_INIT_STRUCT_PTR (&focusControl, OMX_IMAGE_CONFIG_FOCUSCONTROLTYPE);
227        focusControl.eFocusControl = OMX_IMAGE_FocusControlOff;
228
229        eError =  OMX_SetConfig(mCameraAdapterParameters.mHandleComp,
230                                OMX_IndexConfigFocusControl,
231                                &focusControl);
232        if ( OMX_ErrorNone != eError )
233            {
234            CAMHAL_LOGEB("Error while stopping focus 0x%x", eError);
235            return ErrorUtils::omxToAndroidError(eError);
236            }
237        }
238
239    //Query current focus distance after AF is complete
240    updateFocusDistances(mParameters);
241
242    LOG_FUNCTION_NAME_EXIT;
243
244    return ret;
245}
246
247status_t OMXCameraAdapter::getFocusMode(OMX_IMAGE_CONFIG_FOCUSCONTROLTYPE &focusMode)
248{;
249    OMX_ERRORTYPE eError = OMX_ErrorNone;
250
251    LOG_FUNCTION_NAME;
252
253    if ( OMX_StateInvalid == mComponentState ) {
254        CAMHAL_LOGEA("OMX component is in invalid state");
255        return NO_INIT;
256    }
257
258    OMX_INIT_STRUCT_PTR (&focusMode, OMX_IMAGE_CONFIG_FOCUSCONTROLTYPE);
259    focusMode.nPortIndex = mCameraAdapterParameters.mPrevPortIndex;
260
261    eError =  OMX_GetConfig(mCameraAdapterParameters.mHandleComp,
262                            OMX_IndexConfigFocusControl,
263                            &focusMode);
264
265    if ( OMX_ErrorNone != eError ) {
266        CAMHAL_LOGEB("Error while retrieving focus mode 0x%x", eError);
267    }
268
269    LOG_FUNCTION_NAME_EXIT;
270
271    return ErrorUtils::omxToAndroidError(eError);
272}
273
274status_t OMXCameraAdapter::cancelAutoFocus()
275{
276    status_t ret = NO_ERROR;
277    OMX_ERRORTYPE eError = OMX_ErrorNone;
278    OMX_IMAGE_CONFIG_FOCUSCONTROLTYPE focusMode;
279
280    LOG_FUNCTION_NAME;
281    // Unlock 3A locks since they were locked by AF conditionally
282    if( set3ALock(mUserSetExpLock, mUserSetWbLock, OMX_FALSE) != NO_ERROR) {
283      CAMHAL_LOGEA("Error Unlocking 3A locks");
284    }
285    else{
286      CAMHAL_LOGDA("AE/AWB unlocked successfully");
287    }
288
289    ret = getFocusMode(focusMode);
290    if ( NO_ERROR != ret ) {
291        return ret;
292    }
293
294    //Stop the AF only for modes other than CAF  or Inifinity
295    if ( ( focusMode.eFocusControl != OMX_IMAGE_FocusControlAuto ) &&
296         ( focusMode.eFocusControl != ( OMX_IMAGE_FOCUSCONTROLTYPE )
297                 OMX_IMAGE_FocusControlAutoInfinity ) ) {
298        stopAutoFocus();
299        //Signal a dummy AF event so that in case the callback from ducati
300        //does come then it doesnt crash after
301        //exiting this function since eventSem will go out of scope.
302        ret |= SignalEvent(mCameraAdapterParameters.mHandleComp,
303                                    (OMX_EVENTTYPE) OMX_EventIndexSettingChanged,
304                                    OMX_ALL,
305                                    OMX_IndexConfigCommonFocusStatus,
306                                    NULL );
307    }
308
309    // If the apps call #cancelAutoFocus()}, the face callbacks will also resume.
310    pauseFaceDetection(false);
311
312    LOG_FUNCTION_NAME_EXIT;
313
314    return ret;
315
316}
317
318status_t OMXCameraAdapter::setFocusCallback(bool enabled)
319{
320    status_t ret = NO_ERROR;
321    OMX_ERRORTYPE eError = OMX_ErrorNone;
322    OMX_CONFIG_CALLBACKREQUESTTYPE focusRequstCallback;
323
324    LOG_FUNCTION_NAME;
325
326    if ( OMX_StateExecuting != mComponentState )
327        {
328        CAMHAL_LOGEA("OMX component not in executing state");
329        ret = -1;
330        }
331
332    if ( NO_ERROR == ret )
333        {
334
335        OMX_INIT_STRUCT_PTR (&focusRequstCallback, OMX_CONFIG_CALLBACKREQUESTTYPE);
336        focusRequstCallback.nPortIndex = OMX_ALL;
337        focusRequstCallback.nIndex = OMX_IndexConfigCommonFocusStatus;
338
339        if ( enabled )
340            {
341            focusRequstCallback.bEnable = OMX_TRUE;
342            }
343        else
344            {
345            focusRequstCallback.bEnable = OMX_FALSE;
346            }
347
348        eError =  OMX_SetConfig(mCameraAdapterParameters.mHandleComp,
349                                (OMX_INDEXTYPE) OMX_IndexConfigCallbackRequest,
350                                &focusRequstCallback);
351        if ( OMX_ErrorNone != eError )
352            {
353            CAMHAL_LOGEB("Error registering focus callback 0x%x", eError);
354            ret = -1;
355            }
356        else
357            {
358            CAMHAL_LOGDB("Autofocus callback for index 0x%x registered successfully",
359                         OMX_IndexConfigCommonFocusStatus);
360            }
361        }
362
363    LOG_FUNCTION_NAME_EXIT;
364
365    return ret;
366}
367
368status_t OMXCameraAdapter::returnFocusStatus(bool timeoutReached)
369{
370    status_t ret = NO_ERROR;
371    OMX_PARAM_FOCUSSTATUSTYPE eFocusStatus;
372    bool focusStatus = false;
373    BaseCameraAdapter::AdapterState state, nextState;
374    BaseCameraAdapter::getState(state);
375    BaseCameraAdapter::getNextState(nextState);
376
377    LOG_FUNCTION_NAME;
378
379    OMX_INIT_STRUCT(eFocusStatus, OMX_PARAM_FOCUSSTATUSTYPE);
380
381    if( ((AF_ACTIVE & state ) != AF_ACTIVE) && ((AF_ACTIVE & nextState ) != AF_ACTIVE) )
382       {
383        /// We don't send focus callback if focus was not started
384       CAMHAL_LOGDA("Not sending focus callback because focus was not started");
385       return NO_ERROR;
386       }
387
388    if ( NO_ERROR == ret )
389        {
390
391        if ( !timeoutReached )
392            {
393            ret = checkFocus(&eFocusStatus);
394
395            if ( NO_ERROR != ret )
396                {
397                CAMHAL_LOGEA("Focus status check failed!");
398                }
399            }
400        }
401
402    if ( NO_ERROR == ret )
403        {
404
405        if ( timeoutReached )
406            {
407            focusStatus = false;
408            }
409        else
410            {
411            switch (eFocusStatus.eFocusStatus)
412                {
413                    case OMX_FocusStatusReached:
414                        {
415                        focusStatus = true;
416                        break;
417                        }
418                    case OMX_FocusStatusOff:
419                    case OMX_FocusStatusUnableToReach:
420                    case OMX_FocusStatusRequest:
421                    default:
422                        {
423                        focusStatus = false;
424                        break;
425                        }
426                }
427            // Lock CAF after AF call
428            if( set3ALock(mUserSetExpLock, mUserSetWbLock, OMX_TRUE) != NO_ERROR) {
429                CAMHAL_LOGEA("Error Applying 3A locks");
430            } else {
431                CAMHAL_LOGDA("Focus locked. Applied focus locks successfully");
432            }
433
434            stopAutoFocus();
435            }
436        }
437
438    ret =  BaseCameraAdapter::setState(CAMERA_CANCEL_AUTOFOCUS);
439    if ( NO_ERROR == ret )
440        {
441        ret = BaseCameraAdapter::commitState();
442        }
443    else
444        {
445        ret |= BaseCameraAdapter::rollbackState();
446        }
447
448    if ( NO_ERROR == ret )
449        {
450        notifyFocusSubscribers(focusStatus);
451        }
452
453    // After focus, face detection will resume sending face callbacks
454    pauseFaceDetection(false);
455
456    LOG_FUNCTION_NAME_EXIT;
457
458    return ret;
459}
460
461status_t OMXCameraAdapter::checkFocus(OMX_PARAM_FOCUSSTATUSTYPE *eFocusStatus)
462{
463    status_t ret = NO_ERROR;
464    OMX_ERRORTYPE eError = OMX_ErrorNone;
465
466    LOG_FUNCTION_NAME;
467
468    if ( NULL == eFocusStatus )
469        {
470        CAMHAL_LOGEA("Invalid focus status");
471        ret = -EINVAL;
472        }
473
474    if ( OMX_StateExecuting != mComponentState )
475        {
476        CAMHAL_LOGEA("OMX component not in executing state");
477        ret = -EINVAL;
478        }
479
480    if ( NO_ERROR == ret )
481        {
482        OMX_INIT_STRUCT_PTR (eFocusStatus, OMX_PARAM_FOCUSSTATUSTYPE);
483
484        eError = OMX_GetConfig(mCameraAdapterParameters.mHandleComp,
485                               OMX_IndexConfigCommonFocusStatus,
486                               eFocusStatus);
487        if ( OMX_ErrorNone != eError )
488            {
489            CAMHAL_LOGEB("Error while retrieving focus status: 0x%x", eError);
490            ret = -1;
491            }
492        }
493
494    if ( NO_ERROR == ret )
495        {
496        CAMHAL_LOGDB("Focus Status: %d", eFocusStatus->eFocusStatus);
497        }
498
499    LOG_FUNCTION_NAME_EXIT;
500
501    return ret;
502}
503
504status_t OMXCameraAdapter::updateFocusDistances(CameraParameters &params)
505{
506    OMX_U32 focusNear, focusOptimal, focusFar;
507    status_t ret = NO_ERROR;
508
509    LOG_FUNCTION_NAME;
510
511    ret = getFocusDistances(focusNear, focusOptimal, focusFar);
512    if ( NO_ERROR == ret)
513        {
514        ret = addFocusDistances(focusNear, focusOptimal, focusFar, params);
515            if ( NO_ERROR != ret )
516                {
517                CAMHAL_LOGEB("Error in call to addFocusDistances() 0x%x", ret);
518                }
519        }
520    else
521        {
522        CAMHAL_LOGEB("Error in call to getFocusDistances() 0x%x", ret);
523        }
524
525    LOG_FUNCTION_NAME_EXIT;
526
527    return ret;
528}
529
530status_t OMXCameraAdapter::getFocusDistances(OMX_U32 &near,OMX_U32 &optimal, OMX_U32 &far)
531{
532    status_t ret = NO_ERROR;
533    OMX_ERRORTYPE eError;
534
535    OMX_TI_CONFIG_FOCUSDISTANCETYPE focusDist;
536
537    LOG_FUNCTION_NAME;
538
539    if ( OMX_StateInvalid == mComponentState )
540        {
541        CAMHAL_LOGEA("OMX component is in invalid state");
542        ret = UNKNOWN_ERROR;
543        }
544
545    if ( NO_ERROR == ret )
546        {
547        OMX_INIT_STRUCT_PTR(&focusDist, OMX_TI_CONFIG_FOCUSDISTANCETYPE);
548        focusDist.nPortIndex = mCameraAdapterParameters.mPrevPortIndex;
549
550        eError = OMX_GetConfig(mCameraAdapterParameters.mHandleComp,
551                               ( OMX_INDEXTYPE ) OMX_TI_IndexConfigFocusDistance,
552                               &focusDist);
553        if ( OMX_ErrorNone != eError )
554            {
555            CAMHAL_LOGEB("Error while querying focus distances 0x%x", eError);
556            ret = UNKNOWN_ERROR;
557            }
558
559        }
560
561    if ( NO_ERROR == ret )
562        {
563        near = focusDist.nFocusDistanceNear;
564        optimal = focusDist.nFocusDistanceOptimal;
565        far = focusDist.nFocusDistanceFar;
566        }
567
568    LOG_FUNCTION_NAME_EXIT;
569
570    return ret;
571}
572
573status_t OMXCameraAdapter::encodeFocusDistance(OMX_U32 dist, char *buffer, size_t length)
574{
575    status_t ret = NO_ERROR;
576    uint32_t focusScale = 1000;
577    float distFinal;
578
579    LOG_FUNCTION_NAME;
580
581    if(mParameters3A.Focus == OMX_IMAGE_FocusControlAutoInfinity)
582        {
583        dist=0;
584        }
585
586    if ( NO_ERROR == ret )
587        {
588        if ( 0 == dist )
589            {
590            strncpy(buffer, CameraParameters::FOCUS_DISTANCE_INFINITY, ( length - 1 ));
591            }
592        else
593            {
594            distFinal = dist;
595            distFinal /= focusScale;
596            snprintf(buffer, ( length - 1 ) , "%5.3f", distFinal);
597            }
598        }
599
600    LOG_FUNCTION_NAME_EXIT;
601
602    return ret;
603}
604
605status_t OMXCameraAdapter::addFocusDistances(OMX_U32 &near,
606                                             OMX_U32 &optimal,
607                                             OMX_U32 &far,
608                                             CameraParameters& params)
609{
610    status_t ret = NO_ERROR;
611
612    LOG_FUNCTION_NAME;
613
614    if ( NO_ERROR == ret )
615        {
616        ret = encodeFocusDistance(near, mFocusDistNear, FOCUS_DIST_SIZE);
617        if ( NO_ERROR != ret )
618            {
619            CAMHAL_LOGEB("Error encoding near focus distance 0x%x", ret);
620            }
621        }
622
623    if ( NO_ERROR == ret )
624        {
625        ret = encodeFocusDistance(optimal, mFocusDistOptimal, FOCUS_DIST_SIZE);
626        if ( NO_ERROR != ret )
627            {
628            CAMHAL_LOGEB("Error encoding near focus distance 0x%x", ret);
629            }
630        }
631
632    if ( NO_ERROR == ret )
633        {
634        ret = encodeFocusDistance(far, mFocusDistFar, FOCUS_DIST_SIZE);
635        if ( NO_ERROR != ret )
636            {
637            CAMHAL_LOGEB("Error encoding near focus distance 0x%x", ret);
638            }
639        }
640
641    if ( NO_ERROR == ret )
642        {
643        snprintf(mFocusDistBuffer, ( FOCUS_DIST_BUFFER_SIZE - 1) ,"%s,%s,%s", mFocusDistNear,
644                                                                              mFocusDistOptimal,
645                                                                              mFocusDistFar);
646
647        params.set(CameraParameters::KEY_FOCUS_DISTANCES, mFocusDistBuffer);
648        }
649
650    LOG_FUNCTION_NAME_EXIT;
651
652    return ret;
653}
654
655status_t OMXCameraAdapter::setTouchFocus()
656{
657    status_t ret = NO_ERROR;
658    OMX_ERRORTYPE eError = OMX_ErrorNone;
659
660    OMX_ALGOAREASTYPE **focusAreas;
661    OMX_TI_CONFIG_SHAREDBUFFER sharedBuffer;
662    MemoryManager memMgr;
663    int areasSize = 0;
664
665    LOG_FUNCTION_NAME;
666
667    if ( OMX_StateInvalid == mComponentState )
668        {
669        CAMHAL_LOGEA("OMX component is in invalid state");
670        ret = -1;
671        }
672
673    if ( NO_ERROR == ret )
674        {
675
676        areasSize = ((sizeof(OMX_ALGOAREASTYPE)+4095)/4096)*4096;
677        focusAreas = (OMX_ALGOAREASTYPE**) memMgr.allocateBuffer(0, 0, NULL, areasSize, 1);
678
679        OMXCameraPortParameters * mPreviewData = NULL;
680        mPreviewData = &mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mPrevPortIndex];
681
682        if (!focusAreas)
683            {
684            CAMHAL_LOGEB("Error allocating buffer for focus areas %d", eError);
685            return -ENOMEM;
686            }
687
688        OMX_INIT_STRUCT_PTR (focusAreas[0], OMX_ALGOAREASTYPE);
689
690        focusAreas[0]->nPortIndex = OMX_ALL;
691        focusAreas[0]->nNumAreas = mFocusAreas.size();
692        focusAreas[0]->nAlgoAreaPurpose = OMX_AlgoAreaFocus;
693
694        // If the area is the special case of (0, 0, 0, 0, 0), then
695        // the algorithm needs nNumAreas to be set to 0,
696        // in order to automatically choose the best fitting areas.
697        if ( mFocusAreas.itemAt(0)->isZeroArea() )
698            {
699            focusAreas[0]->nNumAreas = 0;
700            }
701
702        for ( unsigned int n = 0; n < mFocusAreas.size(); n++)
703            {
704            // transform the coordinates to 3A-type coordinates
705            mFocusAreas.itemAt(n)->transfrom((size_t)mPreviewData->mWidth,
706                                            (size_t)mPreviewData->mHeight,
707                                            (size_t&)focusAreas[0]->tAlgoAreas[n].nTop,
708                                            (size_t&)focusAreas[0]->tAlgoAreas[n].nLeft,
709                                            (size_t&)focusAreas[0]->tAlgoAreas[n].nWidth,
710                                            (size_t&)focusAreas[0]->tAlgoAreas[n].nHeight);
711
712            focusAreas[0]->tAlgoAreas[n].nLeft =
713                    ( focusAreas[0]->tAlgoAreas[n].nLeft * TOUCH_FOCUS_RANGE ) / mPreviewData->mWidth;
714            focusAreas[0]->tAlgoAreas[n].nTop =
715                    ( focusAreas[0]->tAlgoAreas[n].nTop* TOUCH_FOCUS_RANGE ) / mPreviewData->mHeight;
716            focusAreas[0]->tAlgoAreas[n].nWidth =
717                    ( focusAreas[0]->tAlgoAreas[n].nWidth * TOUCH_FOCUS_RANGE ) / mPreviewData->mWidth;
718            focusAreas[0]->tAlgoAreas[n].nHeight =
719                    ( focusAreas[0]->tAlgoAreas[n].nHeight * TOUCH_FOCUS_RANGE ) / mPreviewData->mHeight;
720            focusAreas[0]->tAlgoAreas[n].nPriority = mFocusAreas.itemAt(n)->getWeight();
721
722             CAMHAL_LOGDB("Focus area %d : top = %d left = %d width = %d height = %d prio = %d",
723                    n, (int)focusAreas[0]->tAlgoAreas[n].nTop, (int)focusAreas[0]->tAlgoAreas[n].nLeft,
724                    (int)focusAreas[0]->tAlgoAreas[n].nWidth, (int)focusAreas[0]->tAlgoAreas[n].nHeight,
725                    (int)focusAreas[0]->tAlgoAreas[n].nPriority);
726             }
727
728        OMX_INIT_STRUCT_PTR (&sharedBuffer, OMX_TI_CONFIG_SHAREDBUFFER);
729
730        sharedBuffer.nPortIndex = OMX_ALL;
731        sharedBuffer.nSharedBuffSize = areasSize;
732        sharedBuffer.pSharedBuff = (OMX_U8 *) focusAreas[0];
733
734        if ( NULL == sharedBuffer.pSharedBuff )
735            {
736            CAMHAL_LOGEA("No resources to allocate OMX shared buffer");
737            ret = -ENOMEM;
738            goto EXIT;
739            }
740
741            eError =  OMX_SetConfig(mCameraAdapterParameters.mHandleComp,
742                                      (OMX_INDEXTYPE) OMX_TI_IndexConfigAlgoAreas, &sharedBuffer);
743
744        if ( OMX_ErrorNone != eError )
745            {
746            CAMHAL_LOGEB("Error while setting Focus Areas configuration 0x%x", eError);
747            ret = -EINVAL;
748            }
749
750    EXIT:
751        if (NULL != focusAreas)
752            {
753            memMgr.freeBuffer((void*) focusAreas);
754            focusAreas = NULL;
755            }
756        }
757
758    LOG_FUNCTION_NAME_EXIT;
759
760    return ret;
761}
762
763};
764