OMXFocus.cpp revision fd705eb8b757a4f3d1d80b736cf20cade04524d7
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    LOG_FUNCTION_NAME_EXIT;
240
241    return ret;
242}
243
244status_t OMXCameraAdapter::getFocusMode(OMX_IMAGE_CONFIG_FOCUSCONTROLTYPE &focusMode)
245{;
246    OMX_ERRORTYPE eError = OMX_ErrorNone;
247
248    LOG_FUNCTION_NAME;
249
250    if ( OMX_StateInvalid == mComponentState ) {
251        CAMHAL_LOGEA("OMX component is in invalid state");
252        return NO_INIT;
253    }
254
255    OMX_INIT_STRUCT_PTR (&focusMode, OMX_IMAGE_CONFIG_FOCUSCONTROLTYPE);
256    focusMode.nPortIndex = mCameraAdapterParameters.mPrevPortIndex;
257
258    eError =  OMX_GetConfig(mCameraAdapterParameters.mHandleComp,
259                            OMX_IndexConfigFocusControl,
260                            &focusMode);
261
262    if ( OMX_ErrorNone != eError ) {
263        CAMHAL_LOGEB("Error while retrieving focus mode 0x%x", eError);
264    }
265
266    LOG_FUNCTION_NAME_EXIT;
267
268    return ErrorUtils::omxToAndroidError(eError);
269}
270
271status_t OMXCameraAdapter::cancelAutoFocus()
272{
273    status_t ret = NO_ERROR;
274    OMX_ERRORTYPE eError = OMX_ErrorNone;
275    OMX_IMAGE_CONFIG_FOCUSCONTROLTYPE focusMode;
276
277    LOG_FUNCTION_NAME;
278
279    ret = getFocusMode(focusMode);
280    if ( NO_ERROR != ret ) {
281        return ret;
282    }
283
284    //Stop the AF only for modes other than CAF  or Inifinity
285    if ( ( focusMode.eFocusControl != OMX_IMAGE_FocusControlAuto ) &&
286         ( focusMode.eFocusControl != ( OMX_IMAGE_FOCUSCONTROLTYPE )
287                 OMX_IMAGE_FocusControlAutoInfinity ) ) {
288        stopAutoFocus();
289        //Signal a dummy AF event so that in case the callback from ducati
290        //does come then it doesnt crash after
291        //exiting this function since eventSem will go out of scope.
292        ret |= SignalEvent(mCameraAdapterParameters.mHandleComp,
293                                    (OMX_EVENTTYPE) OMX_EventIndexSettingChanged,
294                                    OMX_ALL,
295                                    OMX_IndexConfigCommonFocusStatus,
296                                    NULL );
297    } else if (focusMode.eFocusControl == OMX_IMAGE_FocusControlAuto) {
298       // re-apply CAF after unlocking and canceling
299       mPending3Asettings |= SetFocus;
300    }
301
302    // If the apps call #cancelAutoFocus()}, the face callbacks will also resume.
303    pauseFaceDetection(false);
304
305    LOG_FUNCTION_NAME_EXIT;
306
307    return ret;
308
309}
310
311status_t OMXCameraAdapter::setFocusCallback(bool enabled)
312{
313    status_t ret = NO_ERROR;
314    OMX_ERRORTYPE eError = OMX_ErrorNone;
315    OMX_CONFIG_CALLBACKREQUESTTYPE focusRequstCallback;
316
317    LOG_FUNCTION_NAME;
318
319    if ( OMX_StateExecuting != mComponentState )
320        {
321        CAMHAL_LOGEA("OMX component not in executing state");
322        ret = -1;
323        }
324
325    if ( NO_ERROR == ret )
326        {
327
328        OMX_INIT_STRUCT_PTR (&focusRequstCallback, OMX_CONFIG_CALLBACKREQUESTTYPE);
329        focusRequstCallback.nPortIndex = OMX_ALL;
330        focusRequstCallback.nIndex = OMX_IndexConfigCommonFocusStatus;
331
332        if ( enabled )
333            {
334            focusRequstCallback.bEnable = OMX_TRUE;
335            }
336        else
337            {
338            focusRequstCallback.bEnable = OMX_FALSE;
339            }
340
341        eError =  OMX_SetConfig(mCameraAdapterParameters.mHandleComp,
342                                (OMX_INDEXTYPE) OMX_IndexConfigCallbackRequest,
343                                &focusRequstCallback);
344        if ( OMX_ErrorNone != eError )
345            {
346            CAMHAL_LOGEB("Error registering focus callback 0x%x", eError);
347            ret = -1;
348            }
349        else
350            {
351            CAMHAL_LOGDB("Autofocus callback for index 0x%x registered successfully",
352                         OMX_IndexConfigCommonFocusStatus);
353            }
354        }
355
356    LOG_FUNCTION_NAME_EXIT;
357
358    return ret;
359}
360
361status_t OMXCameraAdapter::returnFocusStatus(bool timeoutReached)
362{
363    status_t ret = NO_ERROR;
364    OMX_PARAM_FOCUSSTATUSTYPE eFocusStatus;
365    bool focusStatus = false;
366    BaseCameraAdapter::AdapterState state, nextState;
367    BaseCameraAdapter::getState(state);
368    BaseCameraAdapter::getNextState(nextState);
369
370    LOG_FUNCTION_NAME;
371
372    OMX_INIT_STRUCT(eFocusStatus, OMX_PARAM_FOCUSSTATUSTYPE);
373
374    if( ((AF_ACTIVE & state ) != AF_ACTIVE) && ((AF_ACTIVE & nextState ) != AF_ACTIVE) )
375       {
376        /// We don't send focus callback if focus was not started
377       CAMHAL_LOGDA("Not sending focus callback because focus was not started");
378       return NO_ERROR;
379       }
380
381    if ( NO_ERROR == ret )
382        {
383
384        if ( !timeoutReached )
385            {
386            ret = checkFocus(&eFocusStatus);
387
388            if ( NO_ERROR != ret )
389                {
390                CAMHAL_LOGEA("Focus status check failed!");
391                }
392            }
393        }
394
395    if ( NO_ERROR == ret )
396        {
397
398        if ( timeoutReached )
399            {
400            focusStatus = false;
401            }
402        else
403            {
404            switch (eFocusStatus.eFocusStatus)
405                {
406                    case OMX_FocusStatusReached:
407                        {
408                        focusStatus = true;
409                        break;
410                        }
411                    case OMX_FocusStatusOff:
412                    case OMX_FocusStatusUnableToReach:
413                    case OMX_FocusStatusRequest:
414                    default:
415                        {
416                        focusStatus = false;
417                        break;
418                        }
419                }
420            // Lock CAF after AF call
421            if( set3ALock(mUserSetExpLock, mUserSetWbLock, OMX_TRUE) != NO_ERROR) {
422                CAMHAL_LOGEA("Error Applying 3A locks");
423            } else {
424                CAMHAL_LOGDA("Focus locked. Applied focus locks successfully");
425            }
426
427            stopAutoFocus();
428            }
429        //Query current focus distance after AF is complete
430        updateFocusDistances(mParameters);
431       }
432
433    ret =  BaseCameraAdapter::setState(CAMERA_CANCEL_AUTOFOCUS);
434    if ( NO_ERROR == ret )
435        {
436        ret = BaseCameraAdapter::commitState();
437        }
438    else
439        {
440        ret |= BaseCameraAdapter::rollbackState();
441        }
442
443    if ( NO_ERROR == ret )
444        {
445        notifyFocusSubscribers(focusStatus);
446        }
447
448    // After focus, face detection will resume sending face callbacks
449    pauseFaceDetection(false);
450
451    LOG_FUNCTION_NAME_EXIT;
452
453    return ret;
454}
455
456status_t OMXCameraAdapter::checkFocus(OMX_PARAM_FOCUSSTATUSTYPE *eFocusStatus)
457{
458    status_t ret = NO_ERROR;
459    OMX_ERRORTYPE eError = OMX_ErrorNone;
460
461    LOG_FUNCTION_NAME;
462
463    if ( NULL == eFocusStatus )
464        {
465        CAMHAL_LOGEA("Invalid focus status");
466        ret = -EINVAL;
467        }
468
469    if ( OMX_StateExecuting != mComponentState )
470        {
471        CAMHAL_LOGEA("OMX component not in executing state");
472        ret = -EINVAL;
473        }
474
475    if ( NO_ERROR == ret )
476        {
477        OMX_INIT_STRUCT_PTR (eFocusStatus, OMX_PARAM_FOCUSSTATUSTYPE);
478
479        eError = OMX_GetConfig(mCameraAdapterParameters.mHandleComp,
480                               OMX_IndexConfigCommonFocusStatus,
481                               eFocusStatus);
482        if ( OMX_ErrorNone != eError )
483            {
484            CAMHAL_LOGEB("Error while retrieving focus status: 0x%x", eError);
485            ret = -1;
486            }
487        }
488
489    if ( NO_ERROR == ret )
490        {
491        CAMHAL_LOGDB("Focus Status: %d", eFocusStatus->eFocusStatus);
492        }
493
494    LOG_FUNCTION_NAME_EXIT;
495
496    return ret;
497}
498
499status_t OMXCameraAdapter::updateFocusDistances(CameraParameters &params)
500{
501    OMX_U32 focusNear, focusOptimal, focusFar;
502    status_t ret = NO_ERROR;
503
504    LOG_FUNCTION_NAME;
505
506    ret = getFocusDistances(focusNear, focusOptimal, focusFar);
507    if ( NO_ERROR == ret)
508        {
509        ret = addFocusDistances(focusNear, focusOptimal, focusFar, params);
510            if ( NO_ERROR != ret )
511                {
512                CAMHAL_LOGEB("Error in call to addFocusDistances() 0x%x", ret);
513                }
514        }
515    else
516        {
517        CAMHAL_LOGEB("Error in call to getFocusDistances() 0x%x", ret);
518        }
519
520    LOG_FUNCTION_NAME_EXIT;
521
522    return ret;
523}
524
525status_t OMXCameraAdapter::getFocusDistances(OMX_U32 &near,OMX_U32 &optimal, OMX_U32 &far)
526{
527    status_t ret = NO_ERROR;
528    OMX_ERRORTYPE eError;
529
530    OMX_TI_CONFIG_FOCUSDISTANCETYPE focusDist;
531
532    LOG_FUNCTION_NAME;
533
534    if ( OMX_StateInvalid == mComponentState )
535        {
536        CAMHAL_LOGEA("OMX component is in invalid state");
537        ret = UNKNOWN_ERROR;
538        }
539
540    if ( NO_ERROR == ret )
541        {
542        OMX_INIT_STRUCT_PTR(&focusDist, OMX_TI_CONFIG_FOCUSDISTANCETYPE);
543        focusDist.nPortIndex = mCameraAdapterParameters.mPrevPortIndex;
544
545        eError = OMX_GetConfig(mCameraAdapterParameters.mHandleComp,
546                               ( OMX_INDEXTYPE ) OMX_TI_IndexConfigFocusDistance,
547                               &focusDist);
548        if ( OMX_ErrorNone != eError )
549            {
550            CAMHAL_LOGEB("Error while querying focus distances 0x%x", eError);
551            ret = UNKNOWN_ERROR;
552            }
553
554        }
555
556    if ( NO_ERROR == ret )
557        {
558        near = focusDist.nFocusDistanceNear;
559        optimal = focusDist.nFocusDistanceOptimal;
560        far = focusDist.nFocusDistanceFar;
561        }
562
563    LOG_FUNCTION_NAME_EXIT;
564
565    return ret;
566}
567
568status_t OMXCameraAdapter::encodeFocusDistance(OMX_U32 dist, char *buffer, size_t length)
569{
570    status_t ret = NO_ERROR;
571    uint32_t focusScale = 1000;
572    float distFinal;
573
574    LOG_FUNCTION_NAME;
575
576    if(mParameters3A.Focus == OMX_IMAGE_FocusControlAutoInfinity)
577        {
578        dist=0;
579        }
580
581    if ( NO_ERROR == ret )
582        {
583        if ( 0 == dist )
584            {
585            strncpy(buffer, CameraParameters::FOCUS_DISTANCE_INFINITY, ( length - 1 ));
586            }
587        else
588            {
589            distFinal = dist;
590            distFinal /= focusScale;
591            snprintf(buffer, ( length - 1 ) , "%5.3f", distFinal);
592            }
593        }
594
595    LOG_FUNCTION_NAME_EXIT;
596
597    return ret;
598}
599
600status_t OMXCameraAdapter::addFocusDistances(OMX_U32 &near,
601                                             OMX_U32 &optimal,
602                                             OMX_U32 &far,
603                                             CameraParameters& params)
604{
605    status_t ret = NO_ERROR;
606
607    LOG_FUNCTION_NAME;
608
609    if ( NO_ERROR == ret )
610        {
611        ret = encodeFocusDistance(near, mFocusDistNear, FOCUS_DIST_SIZE);
612        if ( NO_ERROR != ret )
613            {
614            CAMHAL_LOGEB("Error encoding near focus distance 0x%x", ret);
615            }
616        }
617
618    if ( NO_ERROR == ret )
619        {
620        ret = encodeFocusDistance(optimal, mFocusDistOptimal, FOCUS_DIST_SIZE);
621        if ( NO_ERROR != ret )
622            {
623            CAMHAL_LOGEB("Error encoding near focus distance 0x%x", ret);
624            }
625        }
626
627    if ( NO_ERROR == ret )
628        {
629        ret = encodeFocusDistance(far, mFocusDistFar, FOCUS_DIST_SIZE);
630        if ( NO_ERROR != ret )
631            {
632            CAMHAL_LOGEB("Error encoding near focus distance 0x%x", ret);
633            }
634        }
635
636    if ( NO_ERROR == ret )
637        {
638        snprintf(mFocusDistBuffer, ( FOCUS_DIST_BUFFER_SIZE - 1) ,"%s,%s,%s", mFocusDistNear,
639                                                                              mFocusDistOptimal,
640                                                                              mFocusDistFar);
641
642        params.set(CameraParameters::KEY_FOCUS_DISTANCES, mFocusDistBuffer);
643        }
644
645    LOG_FUNCTION_NAME_EXIT;
646
647    return ret;
648}
649
650status_t OMXCameraAdapter::setTouchFocus()
651{
652    status_t ret = NO_ERROR;
653    OMX_ERRORTYPE eError = OMX_ErrorNone;
654
655    OMX_ALGOAREASTYPE **focusAreas;
656    OMX_TI_CONFIG_SHAREDBUFFER sharedBuffer;
657    MemoryManager memMgr;
658    int areasSize = 0;
659
660    LOG_FUNCTION_NAME;
661
662    if ( OMX_StateInvalid == mComponentState )
663        {
664        CAMHAL_LOGEA("OMX component is in invalid state");
665        ret = -1;
666        }
667
668    if ( NO_ERROR == ret )
669        {
670
671        areasSize = ((sizeof(OMX_ALGOAREASTYPE)+4095)/4096)*4096;
672        focusAreas = (OMX_ALGOAREASTYPE**) memMgr.allocateBuffer(0, 0, NULL, areasSize, 1);
673
674        OMXCameraPortParameters * mPreviewData = NULL;
675        mPreviewData = &mCameraAdapterParameters.mCameraPortParams[mCameraAdapterParameters.mPrevPortIndex];
676
677        if (!focusAreas)
678            {
679            CAMHAL_LOGEB("Error allocating buffer for focus areas %d", eError);
680            return -ENOMEM;
681            }
682
683        OMX_INIT_STRUCT_PTR (focusAreas[0], OMX_ALGOAREASTYPE);
684
685        focusAreas[0]->nPortIndex = OMX_ALL;
686        focusAreas[0]->nNumAreas = mFocusAreas.size();
687        focusAreas[0]->nAlgoAreaPurpose = OMX_AlgoAreaFocus;
688
689        // If the area is the special case of (0, 0, 0, 0, 0), then
690        // the algorithm needs nNumAreas to be set to 0,
691        // in order to automatically choose the best fitting areas.
692        if ( mFocusAreas.itemAt(0)->isZeroArea() )
693            {
694            focusAreas[0]->nNumAreas = 0;
695            }
696
697        for ( unsigned int n = 0; n < mFocusAreas.size(); n++)
698            {
699            // transform the coordinates to 3A-type coordinates
700            mFocusAreas.itemAt(n)->transfrom((size_t)mPreviewData->mWidth,
701                                            (size_t)mPreviewData->mHeight,
702                                            (size_t&)focusAreas[0]->tAlgoAreas[n].nTop,
703                                            (size_t&)focusAreas[0]->tAlgoAreas[n].nLeft,
704                                            (size_t&)focusAreas[0]->tAlgoAreas[n].nWidth,
705                                            (size_t&)focusAreas[0]->tAlgoAreas[n].nHeight);
706
707            focusAreas[0]->tAlgoAreas[n].nLeft =
708                    ( focusAreas[0]->tAlgoAreas[n].nLeft * TOUCH_FOCUS_RANGE ) / mPreviewData->mWidth;
709            focusAreas[0]->tAlgoAreas[n].nTop =
710                    ( focusAreas[0]->tAlgoAreas[n].nTop* TOUCH_FOCUS_RANGE ) / mPreviewData->mHeight;
711            focusAreas[0]->tAlgoAreas[n].nWidth =
712                    ( focusAreas[0]->tAlgoAreas[n].nWidth * TOUCH_FOCUS_RANGE ) / mPreviewData->mWidth;
713            focusAreas[0]->tAlgoAreas[n].nHeight =
714                    ( focusAreas[0]->tAlgoAreas[n].nHeight * TOUCH_FOCUS_RANGE ) / mPreviewData->mHeight;
715            focusAreas[0]->tAlgoAreas[n].nPriority = mFocusAreas.itemAt(n)->getWeight();
716
717             CAMHAL_LOGDB("Focus area %d : top = %d left = %d width = %d height = %d prio = %d",
718                    n, (int)focusAreas[0]->tAlgoAreas[n].nTop, (int)focusAreas[0]->tAlgoAreas[n].nLeft,
719                    (int)focusAreas[0]->tAlgoAreas[n].nWidth, (int)focusAreas[0]->tAlgoAreas[n].nHeight,
720                    (int)focusAreas[0]->tAlgoAreas[n].nPriority);
721             }
722
723        OMX_INIT_STRUCT_PTR (&sharedBuffer, OMX_TI_CONFIG_SHAREDBUFFER);
724
725        sharedBuffer.nPortIndex = OMX_ALL;
726        sharedBuffer.nSharedBuffSize = areasSize;
727        sharedBuffer.pSharedBuff = (OMX_U8 *) focusAreas[0];
728
729        if ( NULL == sharedBuffer.pSharedBuff )
730            {
731            CAMHAL_LOGEA("No resources to allocate OMX shared buffer");
732            ret = -ENOMEM;
733            goto EXIT;
734            }
735
736            eError =  OMX_SetConfig(mCameraAdapterParameters.mHandleComp,
737                                      (OMX_INDEXTYPE) OMX_TI_IndexConfigAlgoAreas, &sharedBuffer);
738
739        if ( OMX_ErrorNone != eError )
740            {
741            CAMHAL_LOGEB("Error while setting Focus Areas configuration 0x%x", eError);
742            ret = -EINVAL;
743            }
744
745    EXIT:
746        if (NULL != focusAreas)
747            {
748            memMgr.freeBuffer((void*) focusAreas);
749            focusAreas = NULL;
750            }
751        }
752
753    LOG_FUNCTION_NAME_EXIT;
754
755    return ret;
756}
757
758};
759