blob: 02b899abb353a1071b06ba1bfdd13e79c308f9e7 [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>
Iliyan Malchev8951a972011-04-14 16:55:59 -070024#include <ui/GraphicBuffer.h>
25#include <camera/Camera.h>
26#include <camera/CameraParameters.h>
27#include <system/window.h>
28#include <hardware/camera.h>
29
30namespace android {
31
32typedef void (*notify_callback)(int32_t msgType,
33 int32_t ext1,
34 int32_t ext2,
35 void* user);
36
37typedef void (*data_callback)(int32_t msgType,
38 const sp<IMemory> &dataPtr,
Wu-cheng Liff09ef82011-07-28 05:30:59 +080039 camera_frame_metadata_t *metadata,
Iliyan Malchev8951a972011-04-14 16:55:59 -070040 void* user);
41
42typedef void (*data_callback_timestamp)(nsecs_t timestamp,
43 int32_t msgType,
44 const sp<IMemory> &dataPtr,
45 void *user);
46
47/**
48 * CameraHardwareInterface.h defines the interface to the
49 * camera hardware abstraction layer, used for setting and getting
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080050 * parameters, live previewing, and taking pictures. It is used for
51 * HAL devices with version CAMERA_DEVICE_API_VERSION_1_0 only.
Iliyan Malchev8951a972011-04-14 16:55:59 -070052 *
53 * It is a referenced counted interface with RefBase as its base class.
54 * CameraService calls openCameraHardware() to retrieve a strong pointer to the
55 * instance of this interface and may be called multiple times. The
56 * following steps describe a typical sequence:
57 *
58 * -# After CameraService calls openCameraHardware(), getParameters() and
59 * setParameters() are used to initialize the camera instance.
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080060 * -# startPreview() is called.
Iliyan Malchev8951a972011-04-14 16:55:59 -070061 *
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080062 * Prior to taking a picture, CameraService often calls autofocus(). When auto
Iliyan Malchev8951a972011-04-14 16:55:59 -070063 * focusing has completed, the camera instance sends a CAMERA_MSG_FOCUS notification,
64 * which informs the application whether focusing was successful. The camera instance
65 * only sends this message once and it is up to the application to call autoFocus()
66 * again if refocusing is desired.
67 *
68 * CameraService calls takePicture() to request the camera instance take a
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080069 * picture. At this point, if a shutter, postview, raw, and/or compressed
70 * callback is desired, the corresponding message must be enabled. Any memory
71 * provided in a data callback must be copied if it's needed after returning.
Iliyan Malchev8951a972011-04-14 16:55:59 -070072 */
73
74class CameraHardwareInterface : public virtual RefBase {
75public:
Tyler Luu5861a9a2011-10-06 00:00:03 -050076 CameraHardwareInterface(const char *name)
Iliyan Malchev8951a972011-04-14 16:55:59 -070077 {
78 mDevice = 0;
79 mName = name;
Iliyan Malchev8951a972011-04-14 16:55:59 -070080 }
81
82 ~CameraHardwareInterface()
83 {
Steve Blockdf64d152012-01-04 20:05:49 +000084 ALOGI("Destroying camera %s", mName.string());
Tyler Luu5861a9a2011-10-06 00:00:03 -050085 if(mDevice) {
86 int rc = mDevice->common.close(&mDevice->common);
87 if (rc != OK)
Steve Block29357bc2012-01-06 19:20:56 +000088 ALOGE("Could not close camera %s: %d", mName.string(), rc);
Tyler Luu5861a9a2011-10-06 00:00:03 -050089 }
90 }
91
92 status_t initialize(hw_module_t *module)
93 {
Steve Blockdf64d152012-01-04 20:05:49 +000094 ALOGI("Opening camera %s", mName.string());
Tyler Luu5861a9a2011-10-06 00:00:03 -050095 int rc = module->methods->open(module, mName.string(),
96 (hw_device_t **)&mDevice);
97 if (rc != OK) {
Steve Block29357bc2012-01-06 19:20:56 +000098 ALOGE("Could not open camera %s: %d", mName.string(), rc);
Tyler Luu5861a9a2011-10-06 00:00:03 -050099 return rc;
100 }
101 initHalPreviewWindow();
102 return rc;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700103 }
104
105 /** Set the ANativeWindow to which preview frames are sent */
106 status_t setPreviewWindow(const sp<ANativeWindow>& buf)
107 {
Steve Block3856b092011-10-20 11:56:00 +0100108 ALOGV("%s(%s) buf %p", __FUNCTION__, mName.string(), buf.get());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700109
110 if (mDevice->ops->set_preview_window) {
Apurva Rajguru96db5f22012-07-12 11:34:12 -0700111#ifdef QCOM_HARDWARE
112 ALOGV("%s buf %p mPreviewWindow %p", __FUNCTION__, buf.get(), mPreviewWindow.get());
113 if (mPreviewWindow.get() && (buf.get() != mPreviewWindow.get())) {
114 mDevice->ops->set_preview_window(mDevice, 0);
115 }
116#endif
Iliyan Malchev8951a972011-04-14 16:55:59 -0700117 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 */
Igor Murashkinddf3c502012-10-12 16:56:11 -0700431 status_t dump(int fd, const Vector<String16>& /*args*/) const
Iliyan Malchev8951a972011-04-14 16:55:59 -0700432 {
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);
Ankit Premrajkabf3be122011-11-21 19:25:02 -0800460 if (data != NULL) {
461 sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(data->handle));
462 if (index >= mem->mNumBufs) {
Steve Block29357bc2012-01-06 19:20:56 +0000463 ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700464 index, mem->mNumBufs);
465 return;
Ankit Premrajkabf3be122011-11-21 19:25:02 -0800466 }
467 __this->mDataCb(msg_type, mem->mBuffers[index], metadata, __this->mCbUser);
468 } else {
469 __this->mDataCb(msg_type, NULL, metadata, __this->mCbUser);
Iliyan Malchev26adde82011-06-06 17:21:32 -0700470 }
Iliyan Malchev8951a972011-04-14 16:55:59 -0700471 }
472
473 static void __data_cb_timestamp(nsecs_t timestamp, int32_t msg_type,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700474 const camera_memory_t *data, unsigned index,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700475 void *user)
476 {
Steve Block3856b092011-10-20 11:56:00 +0100477 ALOGV("%s", __FUNCTION__);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700478 CameraHardwareInterface *__this =
479 static_cast<CameraHardwareInterface *>(user);
480 // Start refcounting the heap object from here on. When the clients
481 // drop all references, it will be destroyed (as well as the enclosed
482 // MemoryHeapBase.
483 sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(data->handle));
Iliyan Malchev26adde82011-06-06 17:21:32 -0700484 if (index >= mem->mNumBufs) {
Steve Block29357bc2012-01-06 19:20:56 +0000485 ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700486 index, mem->mNumBufs);
487 return;
488 }
489 __this->mDataCbTimestamp(timestamp, msg_type, mem->mBuffers[index], __this->mCbUser);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700490 }
491
492 // This is a utility class that combines a MemoryHeapBase and a MemoryBase
493 // in one. Since we tend to use them in a one-to-one relationship, this is
494 // handy.
495
Iliyan Malchev26adde82011-06-06 17:21:32 -0700496 class CameraHeapMemory : public RefBase {
Iliyan Malchev8951a972011-04-14 16:55:59 -0700497 public:
Iliyan Malchev26adde82011-06-06 17:21:32 -0700498 CameraHeapMemory(int fd, size_t buf_size, uint_t num_buffers = 1) :
499 mBufSize(buf_size),
500 mNumBufs(num_buffers)
Iliyan Malchev8951a972011-04-14 16:55:59 -0700501 {
Iliyan Malchev26adde82011-06-06 17:21:32 -0700502 mHeap = new MemoryHeapBase(fd, buf_size * num_buffers);
503 commonInitialization();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700504 }
505
Iliyan Malchev26adde82011-06-06 17:21:32 -0700506 CameraHeapMemory(size_t buf_size, uint_t num_buffers = 1) :
507 mBufSize(buf_size),
508 mNumBufs(num_buffers)
509 {
510 mHeap = new MemoryHeapBase(buf_size * num_buffers);
511 commonInitialization();
512 }
513
514 void commonInitialization()
515 {
516 handle.data = mHeap->base();
517 handle.size = mBufSize * mNumBufs;
518 handle.handle = this;
519
520 mBuffers = new sp<MemoryBase>[mNumBufs];
521 for (uint_t i = 0; i < mNumBufs; i++)
522 mBuffers[i] = new MemoryBase(mHeap,
523 i * mBufSize,
524 mBufSize);
525
526 handle.release = __put_memory;
527 }
528
529 virtual ~CameraHeapMemory()
530 {
531 delete [] mBuffers;
532 }
533
534 size_t mBufSize;
535 uint_t mNumBufs;
536 sp<MemoryHeapBase> mHeap;
537 sp<MemoryBase> *mBuffers;
538
Iliyan Malchev8951a972011-04-14 16:55:59 -0700539 camera_memory_t handle;
540 };
541
Iliyan Malchev26adde82011-06-06 17:21:32 -0700542 static camera_memory_t* __get_memory(int fd, size_t buf_size, uint_t num_bufs,
543 void *user __attribute__((unused)))
Iliyan Malchev8951a972011-04-14 16:55:59 -0700544 {
Iliyan Malchev26adde82011-06-06 17:21:32 -0700545 CameraHeapMemory *mem;
546 if (fd < 0)
547 mem = new CameraHeapMemory(buf_size, num_bufs);
548 else
549 mem = new CameraHeapMemory(fd, buf_size, num_bufs);
550 mem->incStrong(mem);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700551 return &mem->handle;
552 }
553
Iliyan Malchev26adde82011-06-06 17:21:32 -0700554 static void __put_memory(camera_memory_t *data)
555 {
556 if (!data)
557 return;
558
559 CameraHeapMemory *mem = static_cast<CameraHeapMemory *>(data->handle);
560 mem->decStrong(mem);
561 }
562
Iliyan Malchev8951a972011-04-14 16:55:59 -0700563 static ANativeWindow *__to_anw(void *user)
564 {
565 CameraHardwareInterface *__this =
566 reinterpret_cast<CameraHardwareInterface *>(user);
567 return __this->mPreviewWindow.get();
568 }
569#define anw(n) __to_anw(((struct camera_preview_window *)n)->user)
570
571 static int __dequeue_buffer(struct preview_stream_ops* w,
Iliyan Malchevafcedc92011-06-10 16:05:23 -0700572 buffer_handle_t** buffer, int *stride)
Iliyan Malchev8951a972011-04-14 16:55:59 -0700573 {
574 int rc;
575 ANativeWindow *a = anw(w);
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700576 ANativeWindowBuffer* anb;
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700577 rc = native_window_dequeue_buffer_and_wait(a, &anb);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700578 if (!rc) {
Sundar Raman1e06f432011-06-17 09:05:09 -0500579 *buffer = &anb->handle;
580 *stride = anb->stride;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700581 }
582 return rc;
583 }
584
585#ifndef container_of
586#define container_of(ptr, type, member) ({ \
587 const typeof(((type *) 0)->member) *__mptr = (ptr); \
588 (type *) ((char *) __mptr - (char *)(&((type *)0)->member)); })
589#endif
590
Sundar Raman1e06f432011-06-17 09:05:09 -0500591 static int __lock_buffer(struct preview_stream_ops* w,
Igor Murashkinddf3c502012-10-12 16:56:11 -0700592 buffer_handle_t* /*buffer*/)
Sundar Raman1e06f432011-06-17 09:05:09 -0500593 {
594 ANativeWindow *a = anw(w);
Igor Murashkinddf3c502012-10-12 16:56:11 -0700595 (void)a;
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700596 return 0;
Sundar Raman1e06f432011-06-17 09:05:09 -0500597 }
598
Iliyan Malchev8951a972011-04-14 16:55:59 -0700599 static int __enqueue_buffer(struct preview_stream_ops* w,
600 buffer_handle_t* buffer)
601 {
602 ANativeWindow *a = anw(w);
603 return a->queueBuffer(a,
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700604 container_of(buffer, ANativeWindowBuffer, handle), -1);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700605 }
606
607 static int __cancel_buffer(struct preview_stream_ops* w,
608 buffer_handle_t* buffer)
609 {
610 ANativeWindow *a = anw(w);
611 return a->cancelBuffer(a,
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700612 container_of(buffer, ANativeWindowBuffer, handle), -1);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700613 }
614
615 static int __set_buffer_count(struct preview_stream_ops* w, int count)
616 {
617 ANativeWindow *a = anw(w);
Iliyan Malchev26adde82011-06-06 17:21:32 -0700618 return native_window_set_buffer_count(a, count);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700619 }
620
621 static int __set_buffers_geometry(struct preview_stream_ops* w,
622 int width, int height, int format)
623 {
624 ANativeWindow *a = anw(w);
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700625 return native_window_set_buffers_geometry(a,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700626 width, height, format);
627 }
628
629 static int __set_crop(struct preview_stream_ops *w,
630 int left, int top, int right, int bottom)
631 {
632 ANativeWindow *a = anw(w);
633 android_native_rect_t crop;
634 crop.left = left;
635 crop.top = top;
636 crop.right = right;
637 crop.bottom = bottom;
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700638 return native_window_set_crop(a, &crop);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700639 }
640
Eino-Ville Talvala9f3d5912011-07-26 14:06:07 -0700641 static int __set_timestamp(struct preview_stream_ops *w,
642 int64_t timestamp) {
643 ANativeWindow *a = anw(w);
644 return native_window_set_buffers_timestamp(a, timestamp);
645 }
646
Iliyan Malchev8951a972011-04-14 16:55:59 -0700647 static int __set_usage(struct preview_stream_ops* w, int usage)
648 {
649 ANativeWindow *a = anw(w);
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700650 return native_window_set_usage(a, usage);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700651 }
652
653 static int __set_swap_interval(struct preview_stream_ops *w, int interval)
654 {
655 ANativeWindow *a = anw(w);
656 return a->setSwapInterval(a, interval);
657 }
658
659 static int __get_min_undequeued_buffer_count(
660 const struct preview_stream_ops *w,
661 int *count)
662 {
663 ANativeWindow *a = anw(w);
664 return a->query(a, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, count);
665 }
666
667 void initHalPreviewWindow()
668 {
669 mHalPreviewWindow.nw.cancel_buffer = __cancel_buffer;
Sundar Raman1e06f432011-06-17 09:05:09 -0500670 mHalPreviewWindow.nw.lock_buffer = __lock_buffer;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700671 mHalPreviewWindow.nw.dequeue_buffer = __dequeue_buffer;
672 mHalPreviewWindow.nw.enqueue_buffer = __enqueue_buffer;
673 mHalPreviewWindow.nw.set_buffer_count = __set_buffer_count;
674 mHalPreviewWindow.nw.set_buffers_geometry = __set_buffers_geometry;
675 mHalPreviewWindow.nw.set_crop = __set_crop;
Eino-Ville Talvala9f3d5912011-07-26 14:06:07 -0700676 mHalPreviewWindow.nw.set_timestamp = __set_timestamp;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700677 mHalPreviewWindow.nw.set_usage = __set_usage;
678 mHalPreviewWindow.nw.set_swap_interval = __set_swap_interval;
679
680 mHalPreviewWindow.nw.get_min_undequeued_buffer_count =
681 __get_min_undequeued_buffer_count;
682 }
683
684 sp<ANativeWindow> mPreviewWindow;
685
686 struct camera_preview_window {
687 struct preview_stream_ops nw;
688 void *user;
689 };
690
691 struct camera_preview_window mHalPreviewWindow;
692
693 notify_callback mNotifyCb;
694 data_callback mDataCb;
695 data_callback_timestamp mDataCbTimestamp;
696 void *mCbUser;
697};
698
699}; // namespace android
700
701#endif