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