Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 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 | #ifndef ANDROID_HARDWARE_CAMERA_HARDWARE_INTERFACE_H |
| 18 | #define ANDROID_HARDWARE_CAMERA_HARDWARE_INTERFACE_H |
| 19 | |
| 20 | #include <binder/IMemory.h> |
| 21 | #include <binder/MemoryBase.h> |
| 22 | #include <binder/MemoryHeapBase.h> |
| 23 | #include <utils/RefBase.h> |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 24 | #include <ui/GraphicBuffer.h> |
| 25 | #include <camera/Camera.h> |
| 26 | #include <camera/CameraParameters.h> |
| 27 | #include <system/window.h> |
| 28 | #include <hardware/camera.h> |
| 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | typedef void (*notify_callback)(int32_t msgType, |
| 33 | int32_t ext1, |
| 34 | int32_t ext2, |
| 35 | void* user); |
| 36 | |
| 37 | typedef void (*data_callback)(int32_t msgType, |
| 38 | const sp<IMemory> &dataPtr, |
Wu-cheng Li | ff09ef8 | 2011-07-28 05:30:59 +0800 | [diff] [blame] | 39 | camera_frame_metadata_t *metadata, |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 40 | void* user); |
| 41 | |
| 42 | typedef void (*data_callback_timestamp)(nsecs_t timestamp, |
| 43 | int32_t msgType, |
| 44 | const sp<IMemory> &dataPtr, |
| 45 | void *user); |
| 46 | |
| 47 | /** |
| 48 | * CameraHardwareInterface.h defines the interface to the |
| 49 | * camera hardware abstraction layer, used for setting and getting |
Eino-Ville Talvala | b99c5b8 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 50 | * parameters, live previewing, and taking pictures. It is used for |
| 51 | * HAL devices with version CAMERA_DEVICE_API_VERSION_1_0 only. |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 52 | * |
| 53 | * It is a referenced counted interface with RefBase as its base class. |
| 54 | * CameraService calls openCameraHardware() to retrieve a strong pointer to the |
| 55 | * instance of this interface and may be called multiple times. The |
| 56 | * following steps describe a typical sequence: |
| 57 | * |
| 58 | * -# After CameraService calls openCameraHardware(), getParameters() and |
| 59 | * setParameters() are used to initialize the camera instance. |
Eino-Ville Talvala | b99c5b8 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 60 | * -# startPreview() is called. |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 61 | * |
Eino-Ville Talvala | b99c5b8 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 62 | * Prior to taking a picture, CameraService often calls autofocus(). When auto |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 63 | * focusing has completed, the camera instance sends a CAMERA_MSG_FOCUS notification, |
| 64 | * which informs the application whether focusing was successful. The camera instance |
| 65 | * only sends this message once and it is up to the application to call autoFocus() |
| 66 | * again if refocusing is desired. |
| 67 | * |
| 68 | * CameraService calls takePicture() to request the camera instance take a |
Eino-Ville Talvala | b99c5b8 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 69 | * picture. At this point, if a shutter, postview, raw, and/or compressed |
| 70 | * callback is desired, the corresponding message must be enabled. Any memory |
| 71 | * provided in a data callback must be copied if it's needed after returning. |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 72 | */ |
| 73 | |
| 74 | class CameraHardwareInterface : public virtual RefBase { |
| 75 | public: |
Eino-Ville Talvala | f735117 | 2016-02-09 12:13:57 -0800 | [diff] [blame] | 76 | CameraHardwareInterface(const char *name): |
| 77 | mDevice(nullptr), |
| 78 | mName(name), |
| 79 | mPreviewScalingMode(NOT_SET), |
| 80 | mPreviewTransform(NOT_SET), |
| 81 | mPreviewWidth(NOT_SET), |
| 82 | mPreviewHeight(NOT_SET), |
| 83 | mPreviewFormat(NOT_SET), |
| 84 | mPreviewUsage(0), |
| 85 | mPreviewSwapInterval(NOT_SET), |
| 86 | mPreviewCrop{NOT_SET,NOT_SET,NOT_SET,NOT_SET} |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 87 | { |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | ~CameraHardwareInterface() |
| 91 | { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 92 | ALOGI("Destroying camera %s", mName.string()); |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 93 | if(mDevice) { |
| 94 | int rc = mDevice->common.close(&mDevice->common); |
| 95 | if (rc != OK) |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 96 | ALOGE("Could not close camera %s: %d", mName.string(), rc); |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 100 | status_t initialize(CameraModule *module) |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 101 | { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 102 | ALOGI("Opening camera %s", mName.string()); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 103 | camera_info info; |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 104 | status_t res = module->getCameraInfo(atoi(mName.string()), &info); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 105 | if (res != OK) { |
| 106 | return res; |
| 107 | } |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 108 | |
| 109 | int rc = OK; |
Chien-Yu Chen | 676b21b | 2015-02-24 10:28:19 -0800 | [diff] [blame] | 110 | if (module->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_3 && |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 111 | info.device_version > CAMERA_DEVICE_API_VERSION_1_0) { |
| 112 | // Open higher version camera device as HAL1.0 device. |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 113 | rc = module->openLegacy(mName.string(), |
| 114 | CAMERA_DEVICE_API_VERSION_1_0, |
| 115 | (hw_device_t **)&mDevice); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 116 | } else { |
Chien-Yu Chen | d231fd6 | 2015-02-25 16:04:22 -0800 | [diff] [blame] | 117 | rc = module->open(mName.string(), (hw_device_t **)&mDevice); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 118 | } |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 119 | if (rc != OK) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 120 | ALOGE("Could not open camera %s: %d", mName.string(), rc); |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 121 | return rc; |
| 122 | } |
| 123 | initHalPreviewWindow(); |
| 124 | return rc; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** Set the ANativeWindow to which preview frames are sent */ |
| 128 | status_t setPreviewWindow(const sp<ANativeWindow>& buf) |
| 129 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 130 | ALOGV("%s(%s) buf %p", __FUNCTION__, mName.string(), buf.get()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 131 | if (mDevice->ops->set_preview_window) { |
| 132 | mPreviewWindow = buf; |
Eino-Ville Talvala | f735117 | 2016-02-09 12:13:57 -0800 | [diff] [blame] | 133 | if (buf != nullptr) { |
| 134 | if (mPreviewScalingMode != NOT_SET) { |
| 135 | setPreviewScalingMode(mPreviewScalingMode); |
| 136 | } |
| 137 | if (mPreviewTransform != NOT_SET) { |
| 138 | setPreviewTransform(mPreviewTransform); |
| 139 | } |
| 140 | } |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 141 | mHalPreviewWindow.user = this; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 142 | ALOGV("%s &mHalPreviewWindow %p mHalPreviewWindow.user %p", __FUNCTION__, |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 143 | &mHalPreviewWindow, mHalPreviewWindow.user); |
| 144 | return mDevice->ops->set_preview_window(mDevice, |
| 145 | buf.get() ? &mHalPreviewWindow.nw : 0); |
| 146 | } |
| 147 | return INVALID_OPERATION; |
| 148 | } |
| 149 | |
Eino-Ville Talvala | f735117 | 2016-02-09 12:13:57 -0800 | [diff] [blame] | 150 | status_t setPreviewScalingMode(int scalingMode) |
| 151 | { |
| 152 | int rc = OK; |
| 153 | mPreviewScalingMode = scalingMode; |
| 154 | if (mPreviewWindow != nullptr) { |
| 155 | rc = native_window_set_scaling_mode(mPreviewWindow.get(), |
| 156 | scalingMode); |
| 157 | } |
| 158 | return rc; |
| 159 | } |
| 160 | |
| 161 | status_t setPreviewTransform(int transform) { |
| 162 | int rc = OK; |
| 163 | mPreviewTransform = transform; |
| 164 | if (mPreviewWindow != nullptr) { |
| 165 | rc = native_window_set_buffers_transform(mPreviewWindow.get(), |
| 166 | mPreviewTransform); |
| 167 | } |
| 168 | return rc; |
| 169 | } |
| 170 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 171 | /** Set the notification and data callbacks */ |
| 172 | void setCallbacks(notify_callback notify_cb, |
| 173 | data_callback data_cb, |
| 174 | data_callback_timestamp data_cb_timestamp, |
| 175 | void* user) |
| 176 | { |
| 177 | mNotifyCb = notify_cb; |
| 178 | mDataCb = data_cb; |
| 179 | mDataCbTimestamp = data_cb_timestamp; |
| 180 | mCbUser = user; |
| 181 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 182 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 183 | |
| 184 | if (mDevice->ops->set_callbacks) { |
| 185 | mDevice->ops->set_callbacks(mDevice, |
| 186 | __notify_cb, |
| 187 | __data_cb, |
| 188 | __data_cb_timestamp, |
| 189 | __get_memory, |
| 190 | this); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * The following three functions all take a msgtype, |
| 196 | * which is a bitmask of the messages defined in |
| 197 | * include/ui/Camera.h |
| 198 | */ |
| 199 | |
| 200 | /** |
| 201 | * Enable a message, or set of messages. |
| 202 | */ |
| 203 | void enableMsgType(int32_t msgType) |
| 204 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 205 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 206 | if (mDevice->ops->enable_msg_type) |
| 207 | mDevice->ops->enable_msg_type(mDevice, msgType); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Disable a message, or a set of messages. |
| 212 | * |
| 213 | * Once received a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), camera hal |
| 214 | * should not rely on its client to call releaseRecordingFrame() to release |
| 215 | * video recording frames sent out by the cameral hal before and after the |
| 216 | * disableMsgType(CAMERA_MSG_VIDEO_FRAME) call. Camera hal clients must not |
| 217 | * modify/access any video recording frame after calling |
| 218 | * disableMsgType(CAMERA_MSG_VIDEO_FRAME). |
| 219 | */ |
| 220 | void disableMsgType(int32_t msgType) |
| 221 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 222 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 223 | if (mDevice->ops->disable_msg_type) |
| 224 | mDevice->ops->disable_msg_type(mDevice, msgType); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Query whether a message, or a set of messages, is enabled. |
| 229 | * Note that this is operates as an AND, if any of the messages |
| 230 | * queried are off, this will return false. |
| 231 | */ |
| 232 | int msgTypeEnabled(int32_t msgType) |
| 233 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 234 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 235 | if (mDevice->ops->msg_type_enabled) |
| 236 | return mDevice->ops->msg_type_enabled(mDevice, msgType); |
| 237 | return false; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Start preview mode. |
| 242 | */ |
| 243 | status_t startPreview() |
| 244 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 245 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 246 | if (mDevice->ops->start_preview) |
| 247 | return mDevice->ops->start_preview(mDevice); |
| 248 | return INVALID_OPERATION; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Stop a previously started preview. |
| 253 | */ |
| 254 | void stopPreview() |
| 255 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 256 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 257 | if (mDevice->ops->stop_preview) |
| 258 | mDevice->ops->stop_preview(mDevice); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Returns true if preview is enabled. |
| 263 | */ |
| 264 | int previewEnabled() |
| 265 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 266 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 267 | if (mDevice->ops->preview_enabled) |
| 268 | return mDevice->ops->preview_enabled(mDevice); |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Request the camera hal to store meta data or real YUV data in |
| 274 | * the video buffers send out via CAMERA_MSG_VIDEO_FRRAME for a |
| 275 | * recording session. If it is not called, the default camera |
| 276 | * hal behavior is to store real YUV data in the video buffers. |
| 277 | * |
| 278 | * This method should be called before startRecording() in order |
| 279 | * to be effective. |
| 280 | * |
| 281 | * If meta data is stored in the video buffers, it is up to the |
| 282 | * receiver of the video buffers to interpret the contents and |
| 283 | * to find the actual frame data with the help of the meta data |
| 284 | * in the buffer. How this is done is outside of the scope of |
| 285 | * this method. |
| 286 | * |
| 287 | * Some camera hal may not support storing meta data in the video |
| 288 | * buffers, but all camera hal should support storing real YUV data |
| 289 | * in the video buffers. If the camera hal does not support storing |
| 290 | * the meta data in the video buffers when it is requested to do |
| 291 | * do, INVALID_OPERATION must be returned. It is very useful for |
| 292 | * the camera hal to pass meta data rather than the actual frame |
| 293 | * data directly to the video encoder, since the amount of the |
| 294 | * uncompressed frame data can be very large if video size is large. |
| 295 | * |
| 296 | * @param enable if true to instruct the camera hal to store |
| 297 | * meta data in the video buffers; false to instruct |
| 298 | * the camera hal to store real YUV data in the video |
| 299 | * buffers. |
| 300 | * |
| 301 | * @return OK on success. |
| 302 | */ |
| 303 | |
| 304 | status_t storeMetaDataInBuffers(int enable) |
| 305 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 306 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 307 | if (mDevice->ops->store_meta_data_in_buffers) |
| 308 | return mDevice->ops->store_meta_data_in_buffers(mDevice, enable); |
| 309 | return enable ? INVALID_OPERATION: OK; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Start record mode. When a record image is available a CAMERA_MSG_VIDEO_FRAME |
| 314 | * message is sent with the corresponding frame. Every record frame must be released |
| 315 | * by a cameral hal client via releaseRecordingFrame() before the client calls |
| 316 | * disableMsgType(CAMERA_MSG_VIDEO_FRAME). After the client calls |
| 317 | * disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's responsibility |
| 318 | * to manage the life-cycle of the video recording frames, and the client must |
| 319 | * not modify/access any video recording frames. |
| 320 | */ |
| 321 | status_t startRecording() |
| 322 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 323 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 324 | if (mDevice->ops->start_recording) |
| 325 | return mDevice->ops->start_recording(mDevice); |
| 326 | return INVALID_OPERATION; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Stop a previously started recording. |
| 331 | */ |
| 332 | void stopRecording() |
| 333 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 334 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 335 | if (mDevice->ops->stop_recording) |
| 336 | mDevice->ops->stop_recording(mDevice); |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Returns true if recording is enabled. |
| 341 | */ |
| 342 | int recordingEnabled() |
| 343 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 344 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 345 | if (mDevice->ops->recording_enabled) |
| 346 | return mDevice->ops->recording_enabled(mDevice); |
| 347 | return false; |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Release a record frame previously returned by CAMERA_MSG_VIDEO_FRAME. |
| 352 | * |
| 353 | * It is camera hal client's responsibility to release video recording |
| 354 | * frames sent out by the camera hal before the camera hal receives |
| 355 | * a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME). After it receives |
| 356 | * the call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's |
| 357 | * responsibility of managing the life-cycle of the video recording |
| 358 | * frames. |
| 359 | */ |
| 360 | void releaseRecordingFrame(const sp<IMemory>& mem) |
| 361 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 362 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 363 | if (mDevice->ops->release_recording_frame) { |
| 364 | ssize_t offset; |
| 365 | size_t size; |
| 366 | sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); |
| 367 | void *data = ((uint8_t *)heap->base()) + offset; |
| 368 | return mDevice->ops->release_recording_frame(mDevice, data); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Start auto focus, the notification callback routine is called |
| 374 | * with CAMERA_MSG_FOCUS once when focusing is complete. autoFocus() |
| 375 | * will be called again if another auto focus is needed. |
| 376 | */ |
| 377 | status_t autoFocus() |
| 378 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 379 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 380 | if (mDevice->ops->auto_focus) |
| 381 | return mDevice->ops->auto_focus(mDevice); |
| 382 | return INVALID_OPERATION; |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Cancels auto-focus function. If the auto-focus is still in progress, |
| 387 | * this function will cancel it. Whether the auto-focus is in progress |
| 388 | * or not, this function will return the focus position to the default. |
| 389 | * If the camera does not support auto-focus, this is a no-op. |
| 390 | */ |
| 391 | status_t cancelAutoFocus() |
| 392 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 393 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 394 | if (mDevice->ops->cancel_auto_focus) |
| 395 | return mDevice->ops->cancel_auto_focus(mDevice); |
| 396 | return INVALID_OPERATION; |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Take a picture. |
| 401 | */ |
| 402 | status_t takePicture() |
| 403 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 404 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 405 | if (mDevice->ops->take_picture) |
| 406 | return mDevice->ops->take_picture(mDevice); |
| 407 | return INVALID_OPERATION; |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Cancel a picture that was started with takePicture. Calling this |
| 412 | * method when no picture is being taken is a no-op. |
| 413 | */ |
| 414 | status_t cancelPicture() |
| 415 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 416 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 417 | if (mDevice->ops->cancel_picture) |
| 418 | return mDevice->ops->cancel_picture(mDevice); |
| 419 | return INVALID_OPERATION; |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Set the camera parameters. This returns BAD_VALUE if any parameter is |
| 424 | * invalid or not supported. */ |
| 425 | status_t setParameters(const CameraParameters ¶ms) |
| 426 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 427 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 428 | if (mDevice->ops->set_parameters) |
| 429 | return mDevice->ops->set_parameters(mDevice, |
| 430 | params.flatten().string()); |
| 431 | return INVALID_OPERATION; |
| 432 | } |
| 433 | |
| 434 | /** Return the camera parameters. */ |
| 435 | CameraParameters getParameters() const |
| 436 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 437 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 438 | CameraParameters parms; |
| 439 | if (mDevice->ops->get_parameters) { |
| 440 | char *temp = mDevice->ops->get_parameters(mDevice); |
| 441 | String8 str_parms(temp); |
Iliyan Malchev | 85fb61e | 2011-07-26 15:56:44 -0700 | [diff] [blame] | 442 | if (mDevice->ops->put_parameters) |
| 443 | mDevice->ops->put_parameters(mDevice, temp); |
| 444 | else |
| 445 | free(temp); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 446 | parms.unflatten(str_parms); |
| 447 | } |
| 448 | return parms; |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * Send command to camera driver. |
| 453 | */ |
| 454 | status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) |
| 455 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 456 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 457 | if (mDevice->ops->send_command) |
| 458 | return mDevice->ops->send_command(mDevice, cmd, arg1, arg2); |
| 459 | return INVALID_OPERATION; |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * Release the hardware resources owned by this object. Note that this is |
| 464 | * *not* done in the destructor. |
| 465 | */ |
| 466 | void release() { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 467 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 468 | if (mDevice->ops->release) |
| 469 | mDevice->ops->release(mDevice); |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * Dump state of the camera hardware |
| 474 | */ |
Igor Murashkin | ddf3c50 | 2012-10-12 16:56:11 -0700 | [diff] [blame] | 475 | status_t dump(int fd, const Vector<String16>& /*args*/) const |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 476 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 477 | ALOGV("%s(%s)", __FUNCTION__, mName.string()); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 478 | if (mDevice->ops->dump) |
| 479 | return mDevice->ops->dump(mDevice, fd); |
| 480 | return OK; // It's fine if the HAL doesn't implement dump() |
| 481 | } |
| 482 | |
| 483 | private: |
| 484 | camera_device_t *mDevice; |
| 485 | String8 mName; |
| 486 | |
| 487 | static void __notify_cb(int32_t msg_type, int32_t ext1, |
| 488 | int32_t ext2, void *user) |
| 489 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 490 | ALOGV("%s", __FUNCTION__); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 491 | CameraHardwareInterface *__this = |
| 492 | static_cast<CameraHardwareInterface *>(user); |
| 493 | __this->mNotifyCb(msg_type, ext1, ext2, __this->mCbUser); |
| 494 | } |
| 495 | |
| 496 | static void __data_cb(int32_t msg_type, |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 497 | const camera_memory_t *data, unsigned int index, |
Wu-cheng Li | ff09ef8 | 2011-07-28 05:30:59 +0800 | [diff] [blame] | 498 | camera_frame_metadata_t *metadata, |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 499 | void *user) |
| 500 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 501 | ALOGV("%s", __FUNCTION__); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 502 | CameraHardwareInterface *__this = |
| 503 | static_cast<CameraHardwareInterface *>(user); |
| 504 | sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(data->handle)); |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 505 | if (index >= mem->mNumBufs) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 506 | ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__, |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 507 | index, mem->mNumBufs); |
| 508 | return; |
| 509 | } |
Wu-cheng Li | ff09ef8 | 2011-07-28 05:30:59 +0800 | [diff] [blame] | 510 | __this->mDataCb(msg_type, mem->mBuffers[index], metadata, __this->mCbUser); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | static void __data_cb_timestamp(nsecs_t timestamp, int32_t msg_type, |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 514 | const camera_memory_t *data, unsigned index, |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 515 | void *user) |
| 516 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 517 | ALOGV("%s", __FUNCTION__); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 518 | CameraHardwareInterface *__this = |
| 519 | static_cast<CameraHardwareInterface *>(user); |
| 520 | // Start refcounting the heap object from here on. When the clients |
| 521 | // drop all references, it will be destroyed (as well as the enclosed |
| 522 | // MemoryHeapBase. |
| 523 | sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(data->handle)); |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 524 | if (index >= mem->mNumBufs) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 525 | ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__, |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 526 | index, mem->mNumBufs); |
| 527 | return; |
| 528 | } |
| 529 | __this->mDataCbTimestamp(timestamp, msg_type, mem->mBuffers[index], __this->mCbUser); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | // This is a utility class that combines a MemoryHeapBase and a MemoryBase |
| 533 | // in one. Since we tend to use them in a one-to-one relationship, this is |
| 534 | // handy. |
| 535 | |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 536 | class CameraHeapMemory : public RefBase { |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 537 | public: |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 538 | CameraHeapMemory(int fd, size_t buf_size, uint_t num_buffers = 1) : |
| 539 | mBufSize(buf_size), |
| 540 | mNumBufs(num_buffers) |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 541 | { |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 542 | mHeap = new MemoryHeapBase(fd, buf_size * num_buffers); |
| 543 | commonInitialization(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 544 | } |
| 545 | |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 546 | CameraHeapMemory(size_t buf_size, uint_t num_buffers = 1) : |
| 547 | mBufSize(buf_size), |
| 548 | mNumBufs(num_buffers) |
| 549 | { |
| 550 | mHeap = new MemoryHeapBase(buf_size * num_buffers); |
| 551 | commonInitialization(); |
| 552 | } |
| 553 | |
| 554 | void commonInitialization() |
| 555 | { |
| 556 | handle.data = mHeap->base(); |
| 557 | handle.size = mBufSize * mNumBufs; |
| 558 | handle.handle = this; |
| 559 | |
| 560 | mBuffers = new sp<MemoryBase>[mNumBufs]; |
| 561 | for (uint_t i = 0; i < mNumBufs; i++) |
| 562 | mBuffers[i] = new MemoryBase(mHeap, |
| 563 | i * mBufSize, |
| 564 | mBufSize); |
| 565 | |
| 566 | handle.release = __put_memory; |
| 567 | } |
| 568 | |
| 569 | virtual ~CameraHeapMemory() |
| 570 | { |
| 571 | delete [] mBuffers; |
| 572 | } |
| 573 | |
| 574 | size_t mBufSize; |
| 575 | uint_t mNumBufs; |
| 576 | sp<MemoryHeapBase> mHeap; |
| 577 | sp<MemoryBase> *mBuffers; |
| 578 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 579 | camera_memory_t handle; |
| 580 | }; |
| 581 | |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 582 | static camera_memory_t* __get_memory(int fd, size_t buf_size, uint_t num_bufs, |
| 583 | void *user __attribute__((unused))) |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 584 | { |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 585 | CameraHeapMemory *mem; |
| 586 | if (fd < 0) |
| 587 | mem = new CameraHeapMemory(buf_size, num_bufs); |
| 588 | else |
| 589 | mem = new CameraHeapMemory(fd, buf_size, num_bufs); |
| 590 | mem->incStrong(mem); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 591 | return &mem->handle; |
| 592 | } |
| 593 | |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 594 | static void __put_memory(camera_memory_t *data) |
| 595 | { |
| 596 | if (!data) |
| 597 | return; |
| 598 | |
| 599 | CameraHeapMemory *mem = static_cast<CameraHeapMemory *>(data->handle); |
| 600 | mem->decStrong(mem); |
| 601 | } |
| 602 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 603 | static ANativeWindow *__to_anw(void *user) |
| 604 | { |
| 605 | CameraHardwareInterface *__this = |
| 606 | reinterpret_cast<CameraHardwareInterface *>(user); |
| 607 | return __this->mPreviewWindow.get(); |
| 608 | } |
| 609 | #define anw(n) __to_anw(((struct camera_preview_window *)n)->user) |
Eino-Ville Talvala | f735117 | 2016-02-09 12:13:57 -0800 | [diff] [blame] | 610 | #define hwi(n) reinterpret_cast<CameraHardwareInterface *>(\ |
| 611 | ((struct camera_preview_window *)n)->user) |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 612 | |
| 613 | static int __dequeue_buffer(struct preview_stream_ops* w, |
Iliyan Malchev | afcedc9 | 2011-06-10 16:05:23 -0700 | [diff] [blame] | 614 | buffer_handle_t** buffer, int *stride) |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 615 | { |
| 616 | int rc; |
| 617 | ANativeWindow *a = anw(w); |
Iliyan Malchev | 8ce2364 | 2011-05-01 11:33:26 -0700 | [diff] [blame] | 618 | ANativeWindowBuffer* anb; |
Jamie Gennis | 1e5b2b3 | 2012-06-13 16:29:51 -0700 | [diff] [blame] | 619 | rc = native_window_dequeue_buffer_and_wait(a, &anb); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 620 | if (!rc) { |
Sundar Raman | 1e06f43 | 2011-06-17 09:05:09 -0500 | [diff] [blame] | 621 | *buffer = &anb->handle; |
| 622 | *stride = anb->stride; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 623 | } |
| 624 | return rc; |
| 625 | } |
| 626 | |
| 627 | #ifndef container_of |
| 628 | #define container_of(ptr, type, member) ({ \ |
Dan Albert | 36802bd | 2014-11-20 11:31:17 -0800 | [diff] [blame] | 629 | const __typeof__(((type *) 0)->member) *__mptr = (ptr); \ |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 630 | (type *) ((char *) __mptr - (char *)(&((type *)0)->member)); }) |
| 631 | #endif |
| 632 | |
Sundar Raman | 1e06f43 | 2011-06-17 09:05:09 -0500 | [diff] [blame] | 633 | static int __lock_buffer(struct preview_stream_ops* w, |
Igor Murashkin | ddf3c50 | 2012-10-12 16:56:11 -0700 | [diff] [blame] | 634 | buffer_handle_t* /*buffer*/) |
Sundar Raman | 1e06f43 | 2011-06-17 09:05:09 -0500 | [diff] [blame] | 635 | { |
| 636 | ANativeWindow *a = anw(w); |
Igor Murashkin | ddf3c50 | 2012-10-12 16:56:11 -0700 | [diff] [blame] | 637 | (void)a; |
Jamie Gennis | 1e5b2b3 | 2012-06-13 16:29:51 -0700 | [diff] [blame] | 638 | return 0; |
Sundar Raman | 1e06f43 | 2011-06-17 09:05:09 -0500 | [diff] [blame] | 639 | } |
| 640 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 641 | static int __enqueue_buffer(struct preview_stream_ops* w, |
| 642 | buffer_handle_t* buffer) |
| 643 | { |
| 644 | ANativeWindow *a = anw(w); |
| 645 | return a->queueBuffer(a, |
Jamie Gennis | 1e5b2b3 | 2012-06-13 16:29:51 -0700 | [diff] [blame] | 646 | container_of(buffer, ANativeWindowBuffer, handle), -1); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | static int __cancel_buffer(struct preview_stream_ops* w, |
| 650 | buffer_handle_t* buffer) |
| 651 | { |
| 652 | ANativeWindow *a = anw(w); |
| 653 | return a->cancelBuffer(a, |
Jamie Gennis | 1e5b2b3 | 2012-06-13 16:29:51 -0700 | [diff] [blame] | 654 | container_of(buffer, ANativeWindowBuffer, handle), -1); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | static int __set_buffer_count(struct preview_stream_ops* w, int count) |
| 658 | { |
| 659 | ANativeWindow *a = anw(w); |
Eino-Ville Talvala | f735117 | 2016-02-09 12:13:57 -0800 | [diff] [blame] | 660 | |
| 661 | if (a != nullptr) { |
| 662 | // Workaround for b/27039775 |
| 663 | // Previously, setting the buffer count would reset the buffer |
| 664 | // queue's flag that allows for all buffers to be dequeued on the |
| 665 | // producer side, instead of just the producer's declared max count, |
| 666 | // if no filled buffers have yet been queued by the producer. This |
| 667 | // reset no longer happens, but some HALs depend on this behavior, |
| 668 | // so it needs to be maintained for HAL backwards compatibility. |
| 669 | // Simulate the prior behavior by disconnecting/reconnecting to the |
| 670 | // window and setting the values again. This has the drawback of |
| 671 | // actually causing memory reallocation, which may not have happened |
| 672 | // in the past. |
| 673 | CameraHardwareInterface *hw = hwi(w); |
| 674 | native_window_api_disconnect(a, NATIVE_WINDOW_API_CAMERA); |
| 675 | native_window_api_connect(a, NATIVE_WINDOW_API_CAMERA); |
| 676 | if (hw->mPreviewScalingMode != NOT_SET) { |
| 677 | native_window_set_scaling_mode(a, hw->mPreviewScalingMode); |
| 678 | } |
| 679 | if (hw->mPreviewTransform != NOT_SET) { |
| 680 | native_window_set_buffers_transform(a, hw->mPreviewTransform); |
| 681 | } |
| 682 | if (hw->mPreviewWidth != NOT_SET) { |
| 683 | native_window_set_buffers_dimensions(a, |
| 684 | hw->mPreviewWidth, hw->mPreviewHeight); |
| 685 | native_window_set_buffers_format(a, hw->mPreviewFormat); |
| 686 | } |
| 687 | if (hw->mPreviewUsage != 0) { |
| 688 | native_window_set_usage(a, hw->mPreviewUsage); |
| 689 | } |
| 690 | if (hw->mPreviewSwapInterval != NOT_SET) { |
| 691 | a->setSwapInterval(a, hw->mPreviewSwapInterval); |
| 692 | } |
| 693 | if (hw->mPreviewCrop.left != NOT_SET) { |
| 694 | native_window_set_crop(a, &(hw->mPreviewCrop)); |
| 695 | } |
| 696 | } |
| 697 | |
Iliyan Malchev | 26adde8 | 2011-06-06 17:21:32 -0700 | [diff] [blame] | 698 | return native_window_set_buffer_count(a, count); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | static int __set_buffers_geometry(struct preview_stream_ops* w, |
| 702 | int width, int height, int format) |
| 703 | { |
Pierre Couillaud | c09cec7 | 2014-07-03 10:55:00 -0700 | [diff] [blame] | 704 | int rc; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 705 | ANativeWindow *a = anw(w); |
Eino-Ville Talvala | f735117 | 2016-02-09 12:13:57 -0800 | [diff] [blame] | 706 | CameraHardwareInterface *hw = hwi(w); |
| 707 | hw->mPreviewWidth = width; |
| 708 | hw->mPreviewHeight = height; |
| 709 | hw->mPreviewFormat = format; |
Pierre Couillaud | c09cec7 | 2014-07-03 10:55:00 -0700 | [diff] [blame] | 710 | rc = native_window_set_buffers_dimensions(a, width, height); |
| 711 | if (!rc) { |
| 712 | rc = native_window_set_buffers_format(a, format); |
| 713 | } |
| 714 | return rc; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | static int __set_crop(struct preview_stream_ops *w, |
| 718 | int left, int top, int right, int bottom) |
| 719 | { |
| 720 | ANativeWindow *a = anw(w); |
Eino-Ville Talvala | f735117 | 2016-02-09 12:13:57 -0800 | [diff] [blame] | 721 | CameraHardwareInterface *hw = hwi(w); |
| 722 | hw->mPreviewCrop.left = left; |
| 723 | hw->mPreviewCrop.top = top; |
| 724 | hw->mPreviewCrop.right = right; |
| 725 | hw->mPreviewCrop.bottom = bottom; |
| 726 | return native_window_set_crop(a, &(hw->mPreviewCrop)); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 727 | } |
| 728 | |
Eino-Ville Talvala | 9f3d591 | 2011-07-26 14:06:07 -0700 | [diff] [blame] | 729 | static int __set_timestamp(struct preview_stream_ops *w, |
| 730 | int64_t timestamp) { |
| 731 | ANativeWindow *a = anw(w); |
| 732 | return native_window_set_buffers_timestamp(a, timestamp); |
| 733 | } |
| 734 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 735 | static int __set_usage(struct preview_stream_ops* w, int usage) |
| 736 | { |
| 737 | ANativeWindow *a = anw(w); |
Eino-Ville Talvala | f735117 | 2016-02-09 12:13:57 -0800 | [diff] [blame] | 738 | CameraHardwareInterface *hw = hwi(w); |
| 739 | hw->mPreviewUsage = usage; |
Iliyan Malchev | 8ce2364 | 2011-05-01 11:33:26 -0700 | [diff] [blame] | 740 | return native_window_set_usage(a, usage); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | static int __set_swap_interval(struct preview_stream_ops *w, int interval) |
| 744 | { |
| 745 | ANativeWindow *a = anw(w); |
Eino-Ville Talvala | f735117 | 2016-02-09 12:13:57 -0800 | [diff] [blame] | 746 | CameraHardwareInterface *hw = hwi(w); |
| 747 | hw->mPreviewSwapInterval = interval; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 748 | return a->setSwapInterval(a, interval); |
| 749 | } |
| 750 | |
| 751 | static int __get_min_undequeued_buffer_count( |
| 752 | const struct preview_stream_ops *w, |
| 753 | int *count) |
| 754 | { |
| 755 | ANativeWindow *a = anw(w); |
| 756 | return a->query(a, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, count); |
| 757 | } |
| 758 | |
| 759 | void initHalPreviewWindow() |
| 760 | { |
| 761 | mHalPreviewWindow.nw.cancel_buffer = __cancel_buffer; |
Sundar Raman | 1e06f43 | 2011-06-17 09:05:09 -0500 | [diff] [blame] | 762 | mHalPreviewWindow.nw.lock_buffer = __lock_buffer; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 763 | mHalPreviewWindow.nw.dequeue_buffer = __dequeue_buffer; |
| 764 | mHalPreviewWindow.nw.enqueue_buffer = __enqueue_buffer; |
| 765 | mHalPreviewWindow.nw.set_buffer_count = __set_buffer_count; |
| 766 | mHalPreviewWindow.nw.set_buffers_geometry = __set_buffers_geometry; |
| 767 | mHalPreviewWindow.nw.set_crop = __set_crop; |
Eino-Ville Talvala | 9f3d591 | 2011-07-26 14:06:07 -0700 | [diff] [blame] | 768 | mHalPreviewWindow.nw.set_timestamp = __set_timestamp; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 769 | mHalPreviewWindow.nw.set_usage = __set_usage; |
| 770 | mHalPreviewWindow.nw.set_swap_interval = __set_swap_interval; |
| 771 | |
| 772 | mHalPreviewWindow.nw.get_min_undequeued_buffer_count = |
| 773 | __get_min_undequeued_buffer_count; |
| 774 | } |
| 775 | |
| 776 | sp<ANativeWindow> mPreviewWindow; |
| 777 | |
| 778 | struct camera_preview_window { |
| 779 | struct preview_stream_ops nw; |
| 780 | void *user; |
| 781 | }; |
| 782 | |
| 783 | struct camera_preview_window mHalPreviewWindow; |
| 784 | |
| 785 | notify_callback mNotifyCb; |
| 786 | data_callback mDataCb; |
| 787 | data_callback_timestamp mDataCbTimestamp; |
| 788 | void *mCbUser; |
Eino-Ville Talvala | f735117 | 2016-02-09 12:13:57 -0800 | [diff] [blame] | 789 | |
| 790 | // Cached values for preview stream parameters |
| 791 | static const int NOT_SET = -1; |
| 792 | int mPreviewScalingMode; |
| 793 | int mPreviewTransform; |
| 794 | int mPreviewWidth; |
| 795 | int mPreviewHeight; |
| 796 | int mPreviewFormat; |
| 797 | int mPreviewUsage; |
| 798 | int mPreviewSwapInterval; |
| 799 | android_native_rect_t mPreviewCrop; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 800 | }; |
| 801 | |
| 802 | }; // namespace android |
| 803 | |
| 804 | #endif |