summaryrefslogtreecommitdiffstats
path: root/camera/OMXCameraAdapter/OMXZoom.cpp
blob: af5c5dbb88ddbb6888c37689e9c1962645eb7e9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
 * Copyright (C) Texas Instruments - http://www.ti.com/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
* @file OMXZoom.cpp
*
* This file contains functionality for handling zoom configurations.
*
*/

#include "CameraHal.h"
#include "OMXCameraAdapter.h"

#ifdef CAMERAHAL_TUNA
namespace {
    // Experimentally determined values that make
    // overlapping bands go away in video mode.
    const int32_t FrontSensorVideoMinZoom = 66816; // 65536 + 1280
    const int32_t BackSensorVideoMinZoom = 66304; // 65536 + 768
}
#endif

namespace Ti {
namespace Camera {

const int32_t OMXCameraAdapter::ZOOM_STEPS [ZOOM_STAGES] =  {
                                65536, 68157, 70124, 72745,
                                75366, 77988, 80609, 83231,
                                86508, 89784, 92406, 95683,
                                99615, 102892, 106168, 110100,
                                114033, 117965, 122552, 126484,
                                131072, 135660, 140247, 145490,
                                150733, 155976, 161219, 167117,
                                173015, 178913, 185467, 192020,
                                198574, 205783, 212992, 220201,
                                228065, 236585, 244449, 252969,
                                262144, 271319, 281149, 290980,
                                300810, 311951, 322437, 334234,
                                346030, 357827, 370934, 384041,
                                397148, 411566, 425984, 441057,
                                456131, 472515, 488899, 506593,
                                524288 };

#ifdef CAMERAHAL_TUNA
int32_t OMXCameraAdapter::getZoomStep(int index)
{
    if (index != 0 || !mPrevZoomModeIsVideo) {
        return OMXCameraAdapter::ZOOM_STEPS[index];
    } else if (mSensorIndex == 1) {
        return FrontSensorVideoMinZoom;
    } else {
        return BackSensorVideoMinZoom;
    }
}
#endif

status_t OMXCameraAdapter::setParametersZoom(const android::CameraParameters &params,
                                             BaseCameraAdapter::AdapterState state)
{
    status_t ret = NO_ERROR;
    android::AutoMutex lock(mZoomLock);

    LOG_FUNCTION_NAME;

    //Immediate zoom should not be avaialable while smooth zoom is running
    if ( ( ZOOM_ACTIVE & state ) != ZOOM_ACTIVE )
        {
        int zoom = params.getInt(android::CameraParameters::KEY_ZOOM);
        if (( zoom >= 0 ) && ( zoom < mMaxZoomSupported )) {
            mTargetZoomIdx = zoom;

            //Immediate zoom should be applied instantly ( CTS requirement )
            mCurrentZoomIdx = mTargetZoomIdx;
            if(!mZoomUpdating) {
                doZoom(mCurrentZoomIdx);
                mZoomUpdating = true;
            } else {
                mZoomUpdate = true;
            }

            CAMHAL_LOGDB("Zoom by App %d", zoom);
            }
        }

    LOG_FUNCTION_NAME_EXIT;

    return ret;
}

status_t OMXCameraAdapter::doZoom(int index)
{
    status_t ret = NO_ERROR;
    OMX_ERRORTYPE eError = OMX_ErrorNone;
    OMX_CONFIG_SCALEFACTORTYPE zoomControl;

    LOG_FUNCTION_NAME;

    if ( OMX_StateInvalid == mComponentState )
        {
        CAMHAL_LOGEA("OMX component is in invalid state");
        ret = -1;
        }

    if (( 0 > index) || ((mMaxZoomSupported - 1 ) < index )) {
        CAMHAL_LOGEB("Zoom index %d out of range", index);
        ret = -EINVAL;
        }

    bool curZoomModeIsVideo = (mCapMode == OMXCameraAdapter::VIDEO_MODE ||
        mCapMode == OMXCameraAdapter::VIDEO_MODE_HQ);

    // When index == 0 and there was a switch between video and non-video mode or between sensors,
    // we do need to perform the zoom, as 0th index is different between video and non-video modes
    // and between different sensors.
    if (mPreviousZoomIndx == index && (index != 0 || (curZoomModeIsVideo == mPrevZoomModeIsVideo &&
                                                      mSensorIndex == mPrevZoomSensorIndex)))
        {
        return NO_ERROR;
        }

    mPrevZoomModeIsVideo = curZoomModeIsVideo;
    mPrevZoomSensorIndex = mSensorIndex;

    if ( NO_ERROR == ret )
        {
        OMX_INIT_STRUCT_PTR (&zoomControl, OMX_CONFIG_SCALEFACTORTYPE);
        zoomControl.nPortIndex = OMX_ALL;
#ifdef CAMERAHAL_TUNA
        zoomControl.xHeight = getZoomStep(index);
        zoomControl.xWidth = zoomControl.xHeight;
#endif

        eError =  OMX_SetConfig(mCameraAdapterParameters.mHandleComp,
                                OMX_IndexConfigCommonDigitalZoom,
                                &zoomControl);
        if ( OMX_ErrorNone != eError )
            {
            CAMHAL_LOGEB("Error while applying digital zoom 0x%x", eError);
            ret = -1;
            }
        else
            {
            CAMHAL_LOGDA("Digital zoom applied successfully");
            mPreviousZoomIndx = index;
            }
        }

    LOG_FUNCTION_NAME_EXIT;

    return ret;
}

status_t OMXCameraAdapter::advanceZoom()
{
    status_t ret = NO_ERROR;
    AdapterState state;
    android::AutoMutex lock(mZoomLock);

    BaseCameraAdapter::getState(state);

    if ( mReturnZoomStatus )
        {
        mCurrentZoomIdx +=mZoomInc;
        mTargetZoomIdx = mCurrentZoomIdx;
        mReturnZoomStatus = false;
        ret = doZoom(mCurrentZoomIdx);
        notifyZoomSubscribers(mCurrentZoomIdx, true);
        }
    else if ( mCurrentZoomIdx != mTargetZoomIdx )
        {
        if ( ZOOM_ACTIVE & state )
            {
            if ( mCurrentZoomIdx < mTargetZoomIdx )
                {
                mZoomInc = 1;
                }
            else
                {
                mZoomInc = -1;
                }

            mCurrentZoomIdx += mZoomInc;
            }
        else
            {
            mCurrentZoomIdx = mTargetZoomIdx;
            }

        ret = doZoom(mCurrentZoomIdx);

        if ( ZOOM_ACTIVE & state )
            {
            if ( mCurrentZoomIdx == mTargetZoomIdx )
                {
                CAMHAL_LOGDB("[Goal Reached] Smooth Zoom notify currentIdx = %d, targetIdx = %d",
                             mCurrentZoomIdx,
                             mTargetZoomIdx);

                if ( NO_ERROR == ret )
                    {

                    ret =  BaseCameraAdapter::setState(CAMERA_STOP_SMOOTH_ZOOM);

                    if ( NO_ERROR == ret )
                        {
                        ret = BaseCameraAdapter::commitState();
                        }
                    else
                        {
                        ret |= BaseCameraAdapter::rollbackState();
                        }

                    }
                mReturnZoomStatus = false;
                notifyZoomSubscribers(mCurrentZoomIdx, true);
                }
            else
                {
                CAMHAL_LOGDB("[Advancing] Smooth Zoom notify currentIdx = %d, targetIdx = %d",
                             mCurrentZoomIdx,
                             mTargetZoomIdx);
                notifyZoomSubscribers(mCurrentZoomIdx, false);
                }
            }
        }
    else if ( (mCurrentZoomIdx == mTargetZoomIdx ) &&
              ( ZOOM_ACTIVE & state ) )
        {
            ret = BaseCameraAdapter::setState(CameraAdapter::CAMERA_STOP_SMOOTH_ZOOM);

            if ( NO_ERROR == ret )
                {
                ret = BaseCameraAdapter::commitState();
                }
            else
                {
                ret |= BaseCameraAdapter::rollbackState();
                }

        }

    if(mZoomUpdate) {
        doZoom(mTargetZoomIdx);
        mZoomUpdate = false;
        mZoomUpdating = true;
    } else {
        mZoomUpdating = false;
    }

    return ret;
}

status_t OMXCameraAdapter::startSmoothZoom(int targetIdx)
{
    status_t ret = NO_ERROR;

    LOG_FUNCTION_NAME;

    android::AutoMutex lock(mZoomLock);

    CAMHAL_LOGDB("Start smooth zoom target = %d, mCurrentIdx = %d",
                 targetIdx,
                 mCurrentZoomIdx);

    if (( targetIdx >= 0 ) && ( targetIdx < mMaxZoomSupported )) {
        mTargetZoomIdx = targetIdx;
        mZoomParameterIdx = mCurrentZoomIdx;
        mReturnZoomStatus = false;
    } else {
        CAMHAL_LOGEB("Smooth value out of range %d!", targetIdx);
        ret = -EINVAL;
    }

    LOG_FUNCTION_NAME_EXIT;

    return ret;
}

status_t OMXCameraAdapter::stopSmoothZoom()
{
    status_t ret = NO_ERROR;
    android::AutoMutex lock(mZoomLock);

    LOG_FUNCTION_NAME;

    if ( mTargetZoomIdx != mCurrentZoomIdx )
        {
        if ( mCurrentZoomIdx < mTargetZoomIdx )
            {
            mZoomInc = 1;
            }
        else
            {
            mZoomInc = -1;
            }
        mReturnZoomStatus = true;
        mReturnZoomStatus = true;
        CAMHAL_LOGDB("Stop smooth zoom mCurrentZoomIdx = %d, mTargetZoomIdx = %d",
                     mCurrentZoomIdx,
                     mTargetZoomIdx);
        }

    LOG_FUNCTION_NAME_EXIT;

    return ret;
}

} // namespace Camera
} // namespace Ti