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