blob: 85f975df93abd93f58d774f5b99432081530a40a [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) {
111 mPreviewWindow = buf;
112 mHalPreviewWindow.user = this;
Steve Block3856b092011-10-20 11:56:00 +0100113 ALOGV("%s &mHalPreviewWindow %p mHalPreviewWindow.user %p", __FUNCTION__,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700114 &mHalPreviewWindow, mHalPreviewWindow.user);
115 return mDevice->ops->set_preview_window(mDevice,
116 buf.get() ? &mHalPreviewWindow.nw : 0);
117 }
118 return INVALID_OPERATION;
119 }
120
121 /** Set the notification and data callbacks */
122 void setCallbacks(notify_callback notify_cb,
123 data_callback data_cb,
124 data_callback_timestamp data_cb_timestamp,
125 void* user)
126 {
127 mNotifyCb = notify_cb;
128 mDataCb = data_cb;
129 mDataCbTimestamp = data_cb_timestamp;
130 mCbUser = user;
131
Steve Block3856b092011-10-20 11:56:00 +0100132 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700133
134 if (mDevice->ops->set_callbacks) {
135 mDevice->ops->set_callbacks(mDevice,
136 __notify_cb,
137 __data_cb,
138 __data_cb_timestamp,
139 __get_memory,
140 this);
141 }
142 }
143
144 /**
145 * The following three functions all take a msgtype,
146 * which is a bitmask of the messages defined in
147 * include/ui/Camera.h
148 */
149
150 /**
151 * Enable a message, or set of messages.
152 */
153 void enableMsgType(int32_t msgType)
154 {
Steve Block3856b092011-10-20 11:56:00 +0100155 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700156 if (mDevice->ops->enable_msg_type)
157 mDevice->ops->enable_msg_type(mDevice, msgType);
158 }
159
160 /**
161 * Disable a message, or a set of messages.
162 *
163 * Once received a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), camera hal
164 * should not rely on its client to call releaseRecordingFrame() to release
165 * video recording frames sent out by the cameral hal before and after the
166 * disableMsgType(CAMERA_MSG_VIDEO_FRAME) call. Camera hal clients must not
167 * modify/access any video recording frame after calling
168 * disableMsgType(CAMERA_MSG_VIDEO_FRAME).
169 */
170 void disableMsgType(int32_t msgType)
171 {
Steve Block3856b092011-10-20 11:56:00 +0100172 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700173 if (mDevice->ops->disable_msg_type)
174 mDevice->ops->disable_msg_type(mDevice, msgType);
175 }
176
177 /**
178 * Query whether a message, or a set of messages, is enabled.
179 * Note that this is operates as an AND, if any of the messages
180 * queried are off, this will return false.
181 */
182 int msgTypeEnabled(int32_t msgType)
183 {
Steve Block3856b092011-10-20 11:56:00 +0100184 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700185 if (mDevice->ops->msg_type_enabled)
186 return mDevice->ops->msg_type_enabled(mDevice, msgType);
187 return false;
188 }
189
190 /**
191 * Start preview mode.
192 */
193 status_t startPreview()
194 {
Steve Block3856b092011-10-20 11:56:00 +0100195 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700196 if (mDevice->ops->start_preview)
197 return mDevice->ops->start_preview(mDevice);
198 return INVALID_OPERATION;
199 }
200
201 /**
202 * Stop a previously started preview.
203 */
204 void stopPreview()
205 {
Steve Block3856b092011-10-20 11:56:00 +0100206 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700207 if (mDevice->ops->stop_preview)
208 mDevice->ops->stop_preview(mDevice);
209 }
210
211 /**
212 * Returns true if preview is enabled.
213 */
214 int previewEnabled()
215 {
Steve Block3856b092011-10-20 11:56:00 +0100216 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700217 if (mDevice->ops->preview_enabled)
218 return mDevice->ops->preview_enabled(mDevice);
219 return false;
220 }
221
222 /**
223 * Request the camera hal to store meta data or real YUV data in
224 * the video buffers send out via CAMERA_MSG_VIDEO_FRRAME for a
225 * recording session. If it is not called, the default camera
226 * hal behavior is to store real YUV data in the video buffers.
227 *
228 * This method should be called before startRecording() in order
229 * to be effective.
230 *
231 * If meta data is stored in the video buffers, it is up to the
232 * receiver of the video buffers to interpret the contents and
233 * to find the actual frame data with the help of the meta data
234 * in the buffer. How this is done is outside of the scope of
235 * this method.
236 *
237 * Some camera hal may not support storing meta data in the video
238 * buffers, but all camera hal should support storing real YUV data
239 * in the video buffers. If the camera hal does not support storing
240 * the meta data in the video buffers when it is requested to do
241 * do, INVALID_OPERATION must be returned. It is very useful for
242 * the camera hal to pass meta data rather than the actual frame
243 * data directly to the video encoder, since the amount of the
244 * uncompressed frame data can be very large if video size is large.
245 *
246 * @param enable if true to instruct the camera hal to store
247 * meta data in the video buffers; false to instruct
248 * the camera hal to store real YUV data in the video
249 * buffers.
250 *
251 * @return OK on success.
252 */
253
254 status_t storeMetaDataInBuffers(int enable)
255 {
Steve Block3856b092011-10-20 11:56:00 +0100256 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700257 if (mDevice->ops->store_meta_data_in_buffers)
258 return mDevice->ops->store_meta_data_in_buffers(mDevice, enable);
259 return enable ? INVALID_OPERATION: OK;
260 }
261
262 /**
263 * Start record mode. When a record image is available a CAMERA_MSG_VIDEO_FRAME
264 * message is sent with the corresponding frame. Every record frame must be released
265 * by a cameral hal client via releaseRecordingFrame() before the client calls
266 * disableMsgType(CAMERA_MSG_VIDEO_FRAME). After the client calls
267 * disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's responsibility
268 * to manage the life-cycle of the video recording frames, and the client must
269 * not modify/access any video recording frames.
270 */
271 status_t startRecording()
272 {
Steve Block3856b092011-10-20 11:56:00 +0100273 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700274 if (mDevice->ops->start_recording)
275 return mDevice->ops->start_recording(mDevice);
276 return INVALID_OPERATION;
277 }
278
279 /**
280 * Stop a previously started recording.
281 */
282 void stopRecording()
283 {
Steve Block3856b092011-10-20 11:56:00 +0100284 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700285 if (mDevice->ops->stop_recording)
286 mDevice->ops->stop_recording(mDevice);
287 }
288
289 /**
290 * Returns true if recording is enabled.
291 */
292 int recordingEnabled()
293 {
Steve Block3856b092011-10-20 11:56:00 +0100294 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700295 if (mDevice->ops->recording_enabled)
296 return mDevice->ops->recording_enabled(mDevice);
297 return false;
298 }
299
300 /**
301 * Release a record frame previously returned by CAMERA_MSG_VIDEO_FRAME.
302 *
303 * It is camera hal client's responsibility to release video recording
304 * frames sent out by the camera hal before the camera hal receives
305 * a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME). After it receives
306 * the call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's
307 * responsibility of managing the life-cycle of the video recording
308 * frames.
309 */
310 void releaseRecordingFrame(const sp<IMemory>& mem)
311 {
Steve Block3856b092011-10-20 11:56:00 +0100312 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700313 if (mDevice->ops->release_recording_frame) {
314 ssize_t offset;
315 size_t size;
316 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
317 void *data = ((uint8_t *)heap->base()) + offset;
318 return mDevice->ops->release_recording_frame(mDevice, data);
319 }
320 }
321
322 /**
323 * Start auto focus, the notification callback routine is called
324 * with CAMERA_MSG_FOCUS once when focusing is complete. autoFocus()
325 * will be called again if another auto focus is needed.
326 */
327 status_t autoFocus()
328 {
Steve Block3856b092011-10-20 11:56:00 +0100329 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700330 if (mDevice->ops->auto_focus)
331 return mDevice->ops->auto_focus(mDevice);
332 return INVALID_OPERATION;
333 }
334
335 /**
336 * Cancels auto-focus function. If the auto-focus is still in progress,
337 * this function will cancel it. Whether the auto-focus is in progress
338 * or not, this function will return the focus position to the default.
339 * If the camera does not support auto-focus, this is a no-op.
340 */
341 status_t cancelAutoFocus()
342 {
Steve Block3856b092011-10-20 11:56:00 +0100343 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700344 if (mDevice->ops->cancel_auto_focus)
345 return mDevice->ops->cancel_auto_focus(mDevice);
346 return INVALID_OPERATION;
347 }
348
349 /**
350 * Take a picture.
351 */
352 status_t takePicture()
353 {
Steve Block3856b092011-10-20 11:56:00 +0100354 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700355 if (mDevice->ops->take_picture)
356 return mDevice->ops->take_picture(mDevice);
357 return INVALID_OPERATION;
358 }
359
360 /**
361 * Cancel a picture that was started with takePicture. Calling this
362 * method when no picture is being taken is a no-op.
363 */
364 status_t cancelPicture()
365 {
Steve Block3856b092011-10-20 11:56:00 +0100366 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700367 if (mDevice->ops->cancel_picture)
368 return mDevice->ops->cancel_picture(mDevice);
369 return INVALID_OPERATION;
370 }
371
372 /**
373 * Set the camera parameters. This returns BAD_VALUE if any parameter is
374 * invalid or not supported. */
375 status_t setParameters(const CameraParameters &params)
376 {
Steve Block3856b092011-10-20 11:56:00 +0100377 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700378 if (mDevice->ops->set_parameters)
379 return mDevice->ops->set_parameters(mDevice,
380 params.flatten().string());
381 return INVALID_OPERATION;
382 }
383
384 /** Return the camera parameters. */
385 CameraParameters getParameters() const
386 {
Steve Block3856b092011-10-20 11:56:00 +0100387 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700388 CameraParameters parms;
389 if (mDevice->ops->get_parameters) {
390 char *temp = mDevice->ops->get_parameters(mDevice);
391 String8 str_parms(temp);
Iliyan Malchev85fb61e2011-07-26 15:56:44 -0700392 if (mDevice->ops->put_parameters)
393 mDevice->ops->put_parameters(mDevice, temp);
394 else
395 free(temp);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700396 parms.unflatten(str_parms);
397 }
398 return parms;
399 }
400
401 /**
402 * Send command to camera driver.
403 */
404 status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
405 {
Steve Block3856b092011-10-20 11:56:00 +0100406 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700407 if (mDevice->ops->send_command)
408 return mDevice->ops->send_command(mDevice, cmd, arg1, arg2);
409 return INVALID_OPERATION;
410 }
411
412 /**
413 * Release the hardware resources owned by this object. Note that this is
414 * *not* done in the destructor.
415 */
416 void release() {
Steve Block3856b092011-10-20 11:56:00 +0100417 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700418 if (mDevice->ops->release)
419 mDevice->ops->release(mDevice);
420 }
421
422 /**
423 * Dump state of the camera hardware
424 */
Igor Murashkinddf3c502012-10-12 16:56:11 -0700425 status_t dump(int fd, const Vector<String16>& /*args*/) const
Iliyan Malchev8951a972011-04-14 16:55:59 -0700426 {
Steve Block3856b092011-10-20 11:56:00 +0100427 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700428 if (mDevice->ops->dump)
429 return mDevice->ops->dump(mDevice, fd);
430 return OK; // It's fine if the HAL doesn't implement dump()
431 }
432
433private:
434 camera_device_t *mDevice;
435 String8 mName;
436
437 static void __notify_cb(int32_t msg_type, int32_t ext1,
438 int32_t ext2, void *user)
439 {
Steve Block3856b092011-10-20 11:56:00 +0100440 ALOGV("%s", __FUNCTION__);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700441 CameraHardwareInterface *__this =
442 static_cast<CameraHardwareInterface *>(user);
443 __this->mNotifyCb(msg_type, ext1, ext2, __this->mCbUser);
444 }
445
446 static void __data_cb(int32_t msg_type,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700447 const camera_memory_t *data, unsigned int index,
Wu-cheng Liff09ef82011-07-28 05:30:59 +0800448 camera_frame_metadata_t *metadata,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700449 void *user)
450 {
Steve Block3856b092011-10-20 11:56:00 +0100451 ALOGV("%s", __FUNCTION__);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700452 CameraHardwareInterface *__this =
453 static_cast<CameraHardwareInterface *>(user);
Ankit Premrajkabf3be122011-11-21 19:25:02 -0800454 if (data != NULL) {
455 sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(data->handle));
456 if (index >= mem->mNumBufs) {
Steve Block29357bc2012-01-06 19:20:56 +0000457 ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700458 index, mem->mNumBufs);
459 return;
Ankit Premrajkabf3be122011-11-21 19:25:02 -0800460 }
461 __this->mDataCb(msg_type, mem->mBuffers[index], metadata, __this->mCbUser);
462 } else {
463 __this->mDataCb(msg_type, NULL, metadata, __this->mCbUser);
Iliyan Malchev26adde82011-06-06 17:21:32 -0700464 }
Iliyan Malchev8951a972011-04-14 16:55:59 -0700465 }
466
467 static void __data_cb_timestamp(nsecs_t timestamp, int32_t msg_type,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700468 const camera_memory_t *data, unsigned index,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700469 void *user)
470 {
Steve Block3856b092011-10-20 11:56:00 +0100471 ALOGV("%s", __FUNCTION__);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700472 CameraHardwareInterface *__this =
473 static_cast<CameraHardwareInterface *>(user);
474 // Start refcounting the heap object from here on. When the clients
475 // drop all references, it will be destroyed (as well as the enclosed
476 // MemoryHeapBase.
477 sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(data->handle));
Iliyan Malchev26adde82011-06-06 17:21:32 -0700478 if (index >= mem->mNumBufs) {
Steve Block29357bc2012-01-06 19:20:56 +0000479 ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700480 index, mem->mNumBufs);
481 return;
482 }
483 __this->mDataCbTimestamp(timestamp, msg_type, mem->mBuffers[index], __this->mCbUser);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700484 }
485
486 // This is a utility class that combines a MemoryHeapBase and a MemoryBase
487 // in one. Since we tend to use them in a one-to-one relationship, this is
488 // handy.
489
Iliyan Malchev26adde82011-06-06 17:21:32 -0700490 class CameraHeapMemory : public RefBase {
Iliyan Malchev8951a972011-04-14 16:55:59 -0700491 public:
Iliyan Malchev26adde82011-06-06 17:21:32 -0700492 CameraHeapMemory(int fd, size_t buf_size, uint_t num_buffers = 1) :
493 mBufSize(buf_size),
494 mNumBufs(num_buffers)
Iliyan Malchev8951a972011-04-14 16:55:59 -0700495 {
Iliyan Malchev26adde82011-06-06 17:21:32 -0700496 mHeap = new MemoryHeapBase(fd, buf_size * num_buffers);
497 commonInitialization();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700498 }
499
Iliyan Malchev26adde82011-06-06 17:21:32 -0700500 CameraHeapMemory(size_t buf_size, uint_t num_buffers = 1) :
501 mBufSize(buf_size),
502 mNumBufs(num_buffers)
503 {
504 mHeap = new MemoryHeapBase(buf_size * num_buffers);
505 commonInitialization();
506 }
507
508 void commonInitialization()
509 {
510 handle.data = mHeap->base();
511 handle.size = mBufSize * mNumBufs;
512 handle.handle = this;
513
514 mBuffers = new sp<MemoryBase>[mNumBufs];
515 for (uint_t i = 0; i < mNumBufs; i++)
516 mBuffers[i] = new MemoryBase(mHeap,
517 i * mBufSize,
518 mBufSize);
519
520 handle.release = __put_memory;
521 }
522
523 virtual ~CameraHeapMemory()
524 {
525 delete [] mBuffers;
526 }
527
528 size_t mBufSize;
529 uint_t mNumBufs;
530 sp<MemoryHeapBase> mHeap;
531 sp<MemoryBase> *mBuffers;
532
Iliyan Malchev8951a972011-04-14 16:55:59 -0700533 camera_memory_t handle;
534 };
535
Iliyan Malchev26adde82011-06-06 17:21:32 -0700536 static camera_memory_t* __get_memory(int fd, size_t buf_size, uint_t num_bufs,
537 void *user __attribute__((unused)))
Iliyan Malchev8951a972011-04-14 16:55:59 -0700538 {
Iliyan Malchev26adde82011-06-06 17:21:32 -0700539 CameraHeapMemory *mem;
540 if (fd < 0)
541 mem = new CameraHeapMemory(buf_size, num_bufs);
542 else
543 mem = new CameraHeapMemory(fd, buf_size, num_bufs);
544 mem->incStrong(mem);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700545 return &mem->handle;
546 }
547
Iliyan Malchev26adde82011-06-06 17:21:32 -0700548 static void __put_memory(camera_memory_t *data)
549 {
550 if (!data)
551 return;
552
553 CameraHeapMemory *mem = static_cast<CameraHeapMemory *>(data->handle);
554 mem->decStrong(mem);
555 }
556
Iliyan Malchev8951a972011-04-14 16:55:59 -0700557 static ANativeWindow *__to_anw(void *user)
558 {
559 CameraHardwareInterface *__this =
560 reinterpret_cast<CameraHardwareInterface *>(user);
561 return __this->mPreviewWindow.get();
562 }
563#define anw(n) __to_anw(((struct camera_preview_window *)n)->user)
564
565 static int __dequeue_buffer(struct preview_stream_ops* w,
Iliyan Malchevafcedc92011-06-10 16:05:23 -0700566 buffer_handle_t** buffer, int *stride)
Iliyan Malchev8951a972011-04-14 16:55:59 -0700567 {
568 int rc;
569 ANativeWindow *a = anw(w);
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700570 ANativeWindowBuffer* anb;
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700571 rc = native_window_dequeue_buffer_and_wait(a, &anb);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700572 if (!rc) {
Sundar Raman1e06f432011-06-17 09:05:09 -0500573 *buffer = &anb->handle;
574 *stride = anb->stride;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700575 }
576 return rc;
577 }
578
579#ifndef container_of
580#define container_of(ptr, type, member) ({ \
581 const typeof(((type *) 0)->member) *__mptr = (ptr); \
582 (type *) ((char *) __mptr - (char *)(&((type *)0)->member)); })
583#endif
584
Sundar Raman1e06f432011-06-17 09:05:09 -0500585 static int __lock_buffer(struct preview_stream_ops* w,
Igor Murashkinddf3c502012-10-12 16:56:11 -0700586 buffer_handle_t* /*buffer*/)
Sundar Raman1e06f432011-06-17 09:05:09 -0500587 {
588 ANativeWindow *a = anw(w);
Igor Murashkinddf3c502012-10-12 16:56:11 -0700589 (void)a;
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700590 return 0;
Sundar Raman1e06f432011-06-17 09:05:09 -0500591 }
592
Iliyan Malchev8951a972011-04-14 16:55:59 -0700593 static int __enqueue_buffer(struct preview_stream_ops* w,
594 buffer_handle_t* buffer)
595 {
596 ANativeWindow *a = anw(w);
597 return a->queueBuffer(a,
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700598 container_of(buffer, ANativeWindowBuffer, handle), -1);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700599 }
600
601 static int __cancel_buffer(struct preview_stream_ops* w,
602 buffer_handle_t* buffer)
603 {
604 ANativeWindow *a = anw(w);
605 return a->cancelBuffer(a,
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700606 container_of(buffer, ANativeWindowBuffer, handle), -1);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700607 }
608
609 static int __set_buffer_count(struct preview_stream_ops* w, int count)
610 {
611 ANativeWindow *a = anw(w);
Iliyan Malchev26adde82011-06-06 17:21:32 -0700612 return native_window_set_buffer_count(a, count);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700613 }
614
615 static int __set_buffers_geometry(struct preview_stream_ops* w,
616 int width, int height, int format)
617 {
618 ANativeWindow *a = anw(w);
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700619 return native_window_set_buffers_geometry(a,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700620 width, height, format);
621 }
622
623 static int __set_crop(struct preview_stream_ops *w,
624 int left, int top, int right, int bottom)
625 {
626 ANativeWindow *a = anw(w);
627 android_native_rect_t crop;
628 crop.left = left;
629 crop.top = top;
630 crop.right = right;
631 crop.bottom = bottom;
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700632 return native_window_set_crop(a, &crop);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700633 }
634
Eino-Ville Talvala9f3d5912011-07-26 14:06:07 -0700635 static int __set_timestamp(struct preview_stream_ops *w,
636 int64_t timestamp) {
637 ANativeWindow *a = anw(w);
638 return native_window_set_buffers_timestamp(a, timestamp);
639 }
640
Iliyan Malchev8951a972011-04-14 16:55:59 -0700641 static int __set_usage(struct preview_stream_ops* w, int usage)
642 {
643 ANativeWindow *a = anw(w);
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700644 return native_window_set_usage(a, usage);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700645 }
646
647 static int __set_swap_interval(struct preview_stream_ops *w, int interval)
648 {
649 ANativeWindow *a = anw(w);
650 return a->setSwapInterval(a, interval);
651 }
652
653 static int __get_min_undequeued_buffer_count(
654 const struct preview_stream_ops *w,
655 int *count)
656 {
657 ANativeWindow *a = anw(w);
658 return a->query(a, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, count);
659 }
660
661 void initHalPreviewWindow()
662 {
663 mHalPreviewWindow.nw.cancel_buffer = __cancel_buffer;
Sundar Raman1e06f432011-06-17 09:05:09 -0500664 mHalPreviewWindow.nw.lock_buffer = __lock_buffer;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700665 mHalPreviewWindow.nw.dequeue_buffer = __dequeue_buffer;
666 mHalPreviewWindow.nw.enqueue_buffer = __enqueue_buffer;
667 mHalPreviewWindow.nw.set_buffer_count = __set_buffer_count;
668 mHalPreviewWindow.nw.set_buffers_geometry = __set_buffers_geometry;
669 mHalPreviewWindow.nw.set_crop = __set_crop;
Eino-Ville Talvala9f3d5912011-07-26 14:06:07 -0700670 mHalPreviewWindow.nw.set_timestamp = __set_timestamp;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700671 mHalPreviewWindow.nw.set_usage = __set_usage;
672 mHalPreviewWindow.nw.set_swap_interval = __set_swap_interval;
673
674 mHalPreviewWindow.nw.get_min_undequeued_buffer_count =
675 __get_min_undequeued_buffer_count;
676 }
677
678 sp<ANativeWindow> mPreviewWindow;
679
680 struct camera_preview_window {
681 struct preview_stream_ops nw;
682 void *user;
683 };
684
685 struct camera_preview_window mHalPreviewWindow;
686
687 notify_callback mNotifyCb;
688 data_callback mDataCb;
689 data_callback_timestamp mDataCbTimestamp;
690 void *mCbUser;
691};
692
693}; // namespace android
694
695#endif