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