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