blob: 78e225f98bbfdb9db9574c6b98373fd251ed7151 [file] [log] [blame]
Iliyan Malchev8951a972011-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>
Iliyan Malchev8951a972011-04-14 16:55:59 -070025#include <ui/GraphicBuffer.h>
26#include <camera/Camera.h>
27#include <camera/CameraParameters.h>
28#include <system/window.h>
29#include <hardware/camera.h>
30
31namespace android {
32
33typedef void (*notify_callback)(int32_t msgType,
34 int32_t ext1,
35 int32_t ext2,
36 void* user);
37
38typedef void (*data_callback)(int32_t msgType,
39 const sp<IMemory> &dataPtr,
Wu-cheng Liff09ef82011-07-28 05:30:59 +080040 camera_frame_metadata_t *metadata,
Iliyan Malchev8951a972011-04-14 16:55:59 -070041 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:
Tyler Luu5861a9a2011-10-06 00:00:03 -050082 CameraHardwareInterface(const char *name)
Iliyan Malchev8951a972011-04-14 16:55:59 -070083 {
84 mDevice = 0;
85 mName = name;
Iliyan Malchev8951a972011-04-14 16:55:59 -070086 }
87
88 ~CameraHardwareInterface()
89 {
Steve Blockdf64d152012-01-04 20:05:49 +000090 ALOGI("Destroying camera %s", mName.string());
Tyler Luu5861a9a2011-10-06 00:00:03 -050091 if(mDevice) {
92 int rc = mDevice->common.close(&mDevice->common);
93 if (rc != OK)
Steve Block29357bc2012-01-06 19:20:56 +000094 ALOGE("Could not close camera %s: %d", mName.string(), rc);
Tyler Luu5861a9a2011-10-06 00:00:03 -050095 }
96 }
97
98 status_t initialize(hw_module_t *module)
99 {
Steve Blockdf64d152012-01-04 20:05:49 +0000100 ALOGI("Opening camera %s", mName.string());
Tyler Luu5861a9a2011-10-06 00:00:03 -0500101 int rc = module->methods->open(module, mName.string(),
102 (hw_device_t **)&mDevice);
103 if (rc != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000104 ALOGE("Could not open camera %s: %d", mName.string(), rc);
Tyler Luu5861a9a2011-10-06 00:00:03 -0500105 return rc;
106 }
107 initHalPreviewWindow();
108 return rc;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700109 }
110
111 /** Set the ANativeWindow to which preview frames are sent */
112 status_t setPreviewWindow(const sp<ANativeWindow>& buf)
113 {
Steve Block3856b092011-10-20 11:56:00 +0100114 ALOGV("%s(%s) buf %p", __FUNCTION__, mName.string(), buf.get());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700115
116 if (mDevice->ops->set_preview_window) {
117 mPreviewWindow = buf;
118 mHalPreviewWindow.user = this;
Steve Block3856b092011-10-20 11:56:00 +0100119 ALOGV("%s &mHalPreviewWindow %p mHalPreviewWindow.user %p", __FUNCTION__,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700120 &mHalPreviewWindow, mHalPreviewWindow.user);
121 return mDevice->ops->set_preview_window(mDevice,
122 buf.get() ? &mHalPreviewWindow.nw : 0);
123 }
124 return INVALID_OPERATION;
125 }
126
127 /** Set the notification and data callbacks */
128 void setCallbacks(notify_callback notify_cb,
129 data_callback data_cb,
130 data_callback_timestamp data_cb_timestamp,
131 void* user)
132 {
133 mNotifyCb = notify_cb;
134 mDataCb = data_cb;
135 mDataCbTimestamp = data_cb_timestamp;
136 mCbUser = user;
137
Steve Block3856b092011-10-20 11:56:00 +0100138 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700139
140 if (mDevice->ops->set_callbacks) {
141 mDevice->ops->set_callbacks(mDevice,
142 __notify_cb,
143 __data_cb,
144 __data_cb_timestamp,
145 __get_memory,
146 this);
147 }
148 }
149
150 /**
151 * The following three functions all take a msgtype,
152 * which is a bitmask of the messages defined in
153 * include/ui/Camera.h
154 */
155
156 /**
157 * Enable a message, or set of messages.
158 */
159 void enableMsgType(int32_t msgType)
160 {
Steve Block3856b092011-10-20 11:56:00 +0100161 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700162 if (mDevice->ops->enable_msg_type)
163 mDevice->ops->enable_msg_type(mDevice, msgType);
164 }
165
166 /**
167 * Disable a message, or a set of messages.
168 *
169 * Once received a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), camera hal
170 * should not rely on its client to call releaseRecordingFrame() to release
171 * video recording frames sent out by the cameral hal before and after the
172 * disableMsgType(CAMERA_MSG_VIDEO_FRAME) call. Camera hal clients must not
173 * modify/access any video recording frame after calling
174 * disableMsgType(CAMERA_MSG_VIDEO_FRAME).
175 */
176 void disableMsgType(int32_t msgType)
177 {
Steve Block3856b092011-10-20 11:56:00 +0100178 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700179 if (mDevice->ops->disable_msg_type)
180 mDevice->ops->disable_msg_type(mDevice, msgType);
181 }
182
183 /**
184 * Query whether a message, or a set of messages, is enabled.
185 * Note that this is operates as an AND, if any of the messages
186 * queried are off, this will return false.
187 */
188 int msgTypeEnabled(int32_t msgType)
189 {
Steve Block3856b092011-10-20 11:56:00 +0100190 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700191 if (mDevice->ops->msg_type_enabled)
192 return mDevice->ops->msg_type_enabled(mDevice, msgType);
193 return false;
194 }
195
196 /**
197 * Start preview mode.
198 */
199 status_t startPreview()
200 {
Steve Block3856b092011-10-20 11:56:00 +0100201 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700202 if (mDevice->ops->start_preview)
203 return mDevice->ops->start_preview(mDevice);
204 return INVALID_OPERATION;
205 }
206
207 /**
208 * Stop a previously started preview.
209 */
210 void stopPreview()
211 {
Steve Block3856b092011-10-20 11:56:00 +0100212 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700213 if (mDevice->ops->stop_preview)
214 mDevice->ops->stop_preview(mDevice);
215 }
216
217 /**
218 * Returns true if preview is enabled.
219 */
220 int previewEnabled()
221 {
Steve Block3856b092011-10-20 11:56:00 +0100222 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700223 if (mDevice->ops->preview_enabled)
224 return mDevice->ops->preview_enabled(mDevice);
225 return false;
226 }
227
228 /**
229 * Request the camera hal to store meta data or real YUV data in
230 * the video buffers send out via CAMERA_MSG_VIDEO_FRRAME for a
231 * recording session. If it is not called, the default camera
232 * hal behavior is to store real YUV data in the video buffers.
233 *
234 * This method should be called before startRecording() in order
235 * to be effective.
236 *
237 * If meta data is stored in the video buffers, it is up to the
238 * receiver of the video buffers to interpret the contents and
239 * to find the actual frame data with the help of the meta data
240 * in the buffer. How this is done is outside of the scope of
241 * this method.
242 *
243 * Some camera hal may not support storing meta data in the video
244 * buffers, but all camera hal should support storing real YUV data
245 * in the video buffers. If the camera hal does not support storing
246 * the meta data in the video buffers when it is requested to do
247 * do, INVALID_OPERATION must be returned. It is very useful for
248 * the camera hal to pass meta data rather than the actual frame
249 * data directly to the video encoder, since the amount of the
250 * uncompressed frame data can be very large if video size is large.
251 *
252 * @param enable if true to instruct the camera hal to store
253 * meta data in the video buffers; false to instruct
254 * the camera hal to store real YUV data in the video
255 * buffers.
256 *
257 * @return OK on success.
258 */
259
260 status_t storeMetaDataInBuffers(int enable)
261 {
Steve Block3856b092011-10-20 11:56:00 +0100262 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700263 if (mDevice->ops->store_meta_data_in_buffers)
264 return mDevice->ops->store_meta_data_in_buffers(mDevice, enable);
265 return enable ? INVALID_OPERATION: OK;
266 }
267
268 /**
269 * Start record mode. When a record image is available a CAMERA_MSG_VIDEO_FRAME
270 * message is sent with the corresponding frame. Every record frame must be released
271 * by a cameral hal client via releaseRecordingFrame() before the client calls
272 * disableMsgType(CAMERA_MSG_VIDEO_FRAME). After the client calls
273 * disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's responsibility
274 * to manage the life-cycle of the video recording frames, and the client must
275 * not modify/access any video recording frames.
276 */
277 status_t startRecording()
278 {
Steve Block3856b092011-10-20 11:56:00 +0100279 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700280 if (mDevice->ops->start_recording)
281 return mDevice->ops->start_recording(mDevice);
282 return INVALID_OPERATION;
283 }
284
285 /**
286 * Stop a previously started recording.
287 */
288 void stopRecording()
289 {
Steve Block3856b092011-10-20 11:56:00 +0100290 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700291 if (mDevice->ops->stop_recording)
292 mDevice->ops->stop_recording(mDevice);
293 }
294
295 /**
296 * Returns true if recording is enabled.
297 */
298 int recordingEnabled()
299 {
Steve Block3856b092011-10-20 11:56:00 +0100300 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700301 if (mDevice->ops->recording_enabled)
302 return mDevice->ops->recording_enabled(mDevice);
303 return false;
304 }
305
306 /**
307 * Release a record frame previously returned by CAMERA_MSG_VIDEO_FRAME.
308 *
309 * It is camera hal client's responsibility to release video recording
310 * frames sent out by the camera hal before the camera hal receives
311 * a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME). After it receives
312 * the call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's
313 * responsibility of managing the life-cycle of the video recording
314 * frames.
315 */
316 void releaseRecordingFrame(const sp<IMemory>& mem)
317 {
Steve Block3856b092011-10-20 11:56:00 +0100318 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700319 if (mDevice->ops->release_recording_frame) {
320 ssize_t offset;
321 size_t size;
322 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
323 void *data = ((uint8_t *)heap->base()) + offset;
324 return mDevice->ops->release_recording_frame(mDevice, data);
325 }
326 }
327
328 /**
329 * Start auto focus, the notification callback routine is called
330 * with CAMERA_MSG_FOCUS once when focusing is complete. autoFocus()
331 * will be called again if another auto focus is needed.
332 */
333 status_t autoFocus()
334 {
Steve Block3856b092011-10-20 11:56:00 +0100335 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700336 if (mDevice->ops->auto_focus)
337 return mDevice->ops->auto_focus(mDevice);
338 return INVALID_OPERATION;
339 }
340
341 /**
342 * Cancels auto-focus function. If the auto-focus is still in progress,
343 * this function will cancel it. Whether the auto-focus is in progress
344 * or not, this function will return the focus position to the default.
345 * If the camera does not support auto-focus, this is a no-op.
346 */
347 status_t cancelAutoFocus()
348 {
Steve Block3856b092011-10-20 11:56:00 +0100349 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700350 if (mDevice->ops->cancel_auto_focus)
351 return mDevice->ops->cancel_auto_focus(mDevice);
352 return INVALID_OPERATION;
353 }
354
355 /**
356 * Take a picture.
357 */
358 status_t takePicture()
359 {
Steve Block3856b092011-10-20 11:56:00 +0100360 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700361 if (mDevice->ops->take_picture)
362 return mDevice->ops->take_picture(mDevice);
363 return INVALID_OPERATION;
364 }
365
366 /**
367 * Cancel a picture that was started with takePicture. Calling this
368 * method when no picture is being taken is a no-op.
369 */
370 status_t cancelPicture()
371 {
Steve Block3856b092011-10-20 11:56:00 +0100372 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700373 if (mDevice->ops->cancel_picture)
374 return mDevice->ops->cancel_picture(mDevice);
375 return INVALID_OPERATION;
376 }
377
378 /**
379 * Set the camera parameters. This returns BAD_VALUE if any parameter is
380 * invalid or not supported. */
381 status_t setParameters(const CameraParameters &params)
382 {
Steve Block3856b092011-10-20 11:56:00 +0100383 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700384 if (mDevice->ops->set_parameters)
385 return mDevice->ops->set_parameters(mDevice,
386 params.flatten().string());
387 return INVALID_OPERATION;
388 }
389
390 /** Return the camera parameters. */
391 CameraParameters getParameters() const
392 {
Steve Block3856b092011-10-20 11:56:00 +0100393 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700394 CameraParameters parms;
395 if (mDevice->ops->get_parameters) {
396 char *temp = mDevice->ops->get_parameters(mDevice);
397 String8 str_parms(temp);
Iliyan Malchev85fb61e2011-07-26 15:56:44 -0700398 if (mDevice->ops->put_parameters)
399 mDevice->ops->put_parameters(mDevice, temp);
400 else
401 free(temp);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700402 parms.unflatten(str_parms);
403 }
404 return parms;
405 }
406
407 /**
408 * Send command to camera driver.
409 */
410 status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
411 {
Steve Block3856b092011-10-20 11:56:00 +0100412 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700413 if (mDevice->ops->send_command)
414 return mDevice->ops->send_command(mDevice, cmd, arg1, arg2);
415 return INVALID_OPERATION;
416 }
417
418 /**
419 * Release the hardware resources owned by this object. Note that this is
420 * *not* done in the destructor.
421 */
422 void release() {
Steve Block3856b092011-10-20 11:56:00 +0100423 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700424 if (mDevice->ops->release)
425 mDevice->ops->release(mDevice);
426 }
427
428 /**
429 * Dump state of the camera hardware
430 */
431 status_t dump(int fd, const Vector<String16>& args) const
432 {
Steve Block3856b092011-10-20 11:56:00 +0100433 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700434 if (mDevice->ops->dump)
435 return mDevice->ops->dump(mDevice, fd);
436 return OK; // It's fine if the HAL doesn't implement dump()
437 }
438
439private:
440 camera_device_t *mDevice;
441 String8 mName;
442
443 static void __notify_cb(int32_t msg_type, int32_t ext1,
444 int32_t ext2, void *user)
445 {
Steve Block3856b092011-10-20 11:56:00 +0100446 ALOGV("%s", __FUNCTION__);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700447 CameraHardwareInterface *__this =
448 static_cast<CameraHardwareInterface *>(user);
449 __this->mNotifyCb(msg_type, ext1, ext2, __this->mCbUser);
450 }
451
452 static void __data_cb(int32_t msg_type,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700453 const camera_memory_t *data, unsigned int index,
Wu-cheng Liff09ef82011-07-28 05:30:59 +0800454 camera_frame_metadata_t *metadata,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700455 void *user)
456 {
Steve Block3856b092011-10-20 11:56:00 +0100457 ALOGV("%s", __FUNCTION__);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700458 CameraHardwareInterface *__this =
459 static_cast<CameraHardwareInterface *>(user);
460 sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(data->handle));
Iliyan Malchev26adde82011-06-06 17:21:32 -0700461 if (index >= mem->mNumBufs) {
Steve Block29357bc2012-01-06 19:20:56 +0000462 ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700463 index, mem->mNumBufs);
464 return;
465 }
Wu-cheng Liff09ef82011-07-28 05:30:59 +0800466 __this->mDataCb(msg_type, mem->mBuffers[index], metadata, __this->mCbUser);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700467 }
468
469 static void __data_cb_timestamp(nsecs_t timestamp, int32_t msg_type,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700470 const camera_memory_t *data, unsigned index,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700471 void *user)
472 {
Steve Block3856b092011-10-20 11:56:00 +0100473 ALOGV("%s", __FUNCTION__);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700474 CameraHardwareInterface *__this =
475 static_cast<CameraHardwareInterface *>(user);
476 // Start refcounting the heap object from here on. When the clients
477 // drop all references, it will be destroyed (as well as the enclosed
478 // MemoryHeapBase.
479 sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(data->handle));
Iliyan Malchev26adde82011-06-06 17:21:32 -0700480 if (index >= mem->mNumBufs) {
Steve Block29357bc2012-01-06 19:20:56 +0000481 ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700482 index, mem->mNumBufs);
483 return;
484 }
485 __this->mDataCbTimestamp(timestamp, msg_type, mem->mBuffers[index], __this->mCbUser);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700486 }
487
488 // This is a utility class that combines a MemoryHeapBase and a MemoryBase
489 // in one. Since we tend to use them in a one-to-one relationship, this is
490 // handy.
491
Iliyan Malchev26adde82011-06-06 17:21:32 -0700492 class CameraHeapMemory : public RefBase {
Iliyan Malchev8951a972011-04-14 16:55:59 -0700493 public:
Iliyan Malchev26adde82011-06-06 17:21:32 -0700494 CameraHeapMemory(int fd, size_t buf_size, uint_t num_buffers = 1) :
495 mBufSize(buf_size),
496 mNumBufs(num_buffers)
Iliyan Malchev8951a972011-04-14 16:55:59 -0700497 {
Iliyan Malchev26adde82011-06-06 17:21:32 -0700498 mHeap = new MemoryHeapBase(fd, buf_size * num_buffers);
499 commonInitialization();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700500 }
501
Iliyan Malchev26adde82011-06-06 17:21:32 -0700502 CameraHeapMemory(size_t buf_size, uint_t num_buffers = 1) :
503 mBufSize(buf_size),
504 mNumBufs(num_buffers)
505 {
506 mHeap = new MemoryHeapBase(buf_size * num_buffers);
507 commonInitialization();
508 }
509
510 void commonInitialization()
511 {
512 handle.data = mHeap->base();
513 handle.size = mBufSize * mNumBufs;
514 handle.handle = this;
515
516 mBuffers = new sp<MemoryBase>[mNumBufs];
517 for (uint_t i = 0; i < mNumBufs; i++)
518 mBuffers[i] = new MemoryBase(mHeap,
519 i * mBufSize,
520 mBufSize);
521
522 handle.release = __put_memory;
523 }
524
525 virtual ~CameraHeapMemory()
526 {
527 delete [] mBuffers;
528 }
529
530 size_t mBufSize;
531 uint_t mNumBufs;
532 sp<MemoryHeapBase> mHeap;
533 sp<MemoryBase> *mBuffers;
534
Iliyan Malchev8951a972011-04-14 16:55:59 -0700535 camera_memory_t handle;
536 };
537
Iliyan Malchev26adde82011-06-06 17:21:32 -0700538 static camera_memory_t* __get_memory(int fd, size_t buf_size, uint_t num_bufs,
539 void *user __attribute__((unused)))
Iliyan Malchev8951a972011-04-14 16:55:59 -0700540 {
Iliyan Malchev26adde82011-06-06 17:21:32 -0700541 CameraHeapMemory *mem;
542 if (fd < 0)
543 mem = new CameraHeapMemory(buf_size, num_bufs);
544 else
545 mem = new CameraHeapMemory(fd, buf_size, num_bufs);
546 mem->incStrong(mem);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700547 return &mem->handle;
548 }
549
Iliyan Malchev26adde82011-06-06 17:21:32 -0700550 static void __put_memory(camera_memory_t *data)
551 {
552 if (!data)
553 return;
554
555 CameraHeapMemory *mem = static_cast<CameraHeapMemory *>(data->handle);
556 mem->decStrong(mem);
557 }
558
Iliyan Malchev8951a972011-04-14 16:55:59 -0700559 static ANativeWindow *__to_anw(void *user)
560 {
561 CameraHardwareInterface *__this =
562 reinterpret_cast<CameraHardwareInterface *>(user);
563 return __this->mPreviewWindow.get();
564 }
565#define anw(n) __to_anw(((struct camera_preview_window *)n)->user)
566
567 static int __dequeue_buffer(struct preview_stream_ops* w,
Iliyan Malchevafcedc92011-06-10 16:05:23 -0700568 buffer_handle_t** buffer, int *stride)
Iliyan Malchev8951a972011-04-14 16:55:59 -0700569 {
570 int rc;
571 ANativeWindow *a = anw(w);
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700572 ANativeWindowBuffer* anb;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700573 rc = a->dequeueBuffer(a, &anb);
574 if (!rc) {
Sundar Raman1e06f432011-06-17 09:05:09 -0500575 *buffer = &anb->handle;
576 *stride = anb->stride;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700577 }
578 return rc;
579 }
580
581#ifndef container_of
582#define container_of(ptr, type, member) ({ \
583 const typeof(((type *) 0)->member) *__mptr = (ptr); \
584 (type *) ((char *) __mptr - (char *)(&((type *)0)->member)); })
585#endif
586
Sundar Raman1e06f432011-06-17 09:05:09 -0500587 static int __lock_buffer(struct preview_stream_ops* w,
588 buffer_handle_t* buffer)
589 {
590 ANativeWindow *a = anw(w);
591 return a->lockBuffer(a,
592 container_of(buffer, ANativeWindowBuffer, handle));
593 }
594
Iliyan Malchev8951a972011-04-14 16:55:59 -0700595 static int __enqueue_buffer(struct preview_stream_ops* w,
596 buffer_handle_t* buffer)
597 {
598 ANativeWindow *a = anw(w);
599 return a->queueBuffer(a,
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700600 container_of(buffer, ANativeWindowBuffer, handle));
Iliyan Malchev8951a972011-04-14 16:55:59 -0700601 }
602
603 static int __cancel_buffer(struct preview_stream_ops* w,
604 buffer_handle_t* buffer)
605 {
606 ANativeWindow *a = anw(w);
607 return a->cancelBuffer(a,
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700608 container_of(buffer, ANativeWindowBuffer, handle));
Iliyan Malchev8951a972011-04-14 16:55:59 -0700609 }
610
611 static int __set_buffer_count(struct preview_stream_ops* w, int count)
612 {
613 ANativeWindow *a = anw(w);
Iliyan Malchev26adde82011-06-06 17:21:32 -0700614 return native_window_set_buffer_count(a, count);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700615 }
616
617 static int __set_buffers_geometry(struct preview_stream_ops* w,
618 int width, int height, int format)
619 {
620 ANativeWindow *a = anw(w);
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700621 return native_window_set_buffers_geometry(a,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700622 width, height, format);
623 }
624
625 static int __set_crop(struct preview_stream_ops *w,
626 int left, int top, int right, int bottom)
627 {
628 ANativeWindow *a = anw(w);
629 android_native_rect_t crop;
630 crop.left = left;
631 crop.top = top;
632 crop.right = right;
633 crop.bottom = bottom;
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700634 return native_window_set_crop(a, &crop);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700635 }
636
Eino-Ville Talvala9f3d5912011-07-26 14:06:07 -0700637 static int __set_timestamp(struct preview_stream_ops *w,
638 int64_t timestamp) {
639 ANativeWindow *a = anw(w);
640 return native_window_set_buffers_timestamp(a, timestamp);
641 }
642
Iliyan Malchev8951a972011-04-14 16:55:59 -0700643 static int __set_usage(struct preview_stream_ops* w, int usage)
644 {
645 ANativeWindow *a = anw(w);
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700646 return native_window_set_usage(a, usage);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700647 }
648
649 static int __set_swap_interval(struct preview_stream_ops *w, int interval)
650 {
651 ANativeWindow *a = anw(w);
652 return a->setSwapInterval(a, interval);
653 }
654
655 static int __get_min_undequeued_buffer_count(
656 const struct preview_stream_ops *w,
657 int *count)
658 {
659 ANativeWindow *a = anw(w);
660 return a->query(a, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, count);
661 }
662
663 void initHalPreviewWindow()
664 {
665 mHalPreviewWindow.nw.cancel_buffer = __cancel_buffer;
Sundar Raman1e06f432011-06-17 09:05:09 -0500666 mHalPreviewWindow.nw.lock_buffer = __lock_buffer;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700667 mHalPreviewWindow.nw.dequeue_buffer = __dequeue_buffer;
668 mHalPreviewWindow.nw.enqueue_buffer = __enqueue_buffer;
669 mHalPreviewWindow.nw.set_buffer_count = __set_buffer_count;
670 mHalPreviewWindow.nw.set_buffers_geometry = __set_buffers_geometry;
671 mHalPreviewWindow.nw.set_crop = __set_crop;
Eino-Ville Talvala9f3d5912011-07-26 14:06:07 -0700672 mHalPreviewWindow.nw.set_timestamp = __set_timestamp;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700673 mHalPreviewWindow.nw.set_usage = __set_usage;
674 mHalPreviewWindow.nw.set_swap_interval = __set_swap_interval;
675
676 mHalPreviewWindow.nw.get_min_undequeued_buffer_count =
677 __get_min_undequeued_buffer_count;
678 }
679
680 sp<ANativeWindow> mPreviewWindow;
681
682 struct camera_preview_window {
683 struct preview_stream_ops nw;
684 void *user;
685 };
686
687 struct camera_preview_window mHalPreviewWindow;
688
689 notify_callback mNotifyCb;
690 data_callback mDataCb;
691 data_callback_timestamp mDataCbTimestamp;
692 void *mCbUser;
693};
694
695}; // namespace android
696
697#endif