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