blob: 63868384f0377caa7b18d34027dfab4c16e24b14 [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());
Zhijun Heb10cdad2014-06-16 16:38:35 -070095 camera_module_t *cameraModule = reinterpret_cast<camera_module_t *>(module);
96 camera_info info;
97 status_t res = cameraModule->get_camera_info(atoi(mName.string()), &info);
98 if (res != OK) return res;
99
100 int rc = OK;
101 if (module->module_api_version >= CAMERA_MODULE_API_VERSION_2_3 &&
102 info.device_version > CAMERA_DEVICE_API_VERSION_1_0) {
103 // Open higher version camera device as HAL1.0 device.
104 rc = cameraModule->open_legacy(module, mName.string(),
105 CAMERA_DEVICE_API_VERSION_1_0,
106 (hw_device_t **)&mDevice);
107 } else {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700108 rc = CameraService::filterOpenErrorCode(module->methods->open(
109 module, mName.string(), (hw_device_t **)&mDevice));
Zhijun Heb10cdad2014-06-16 16:38:35 -0700110 }
Tyler Luu5861a9a2011-10-06 00:00:03 -0500111 if (rc != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000112 ALOGE("Could not open camera %s: %d", mName.string(), rc);
Tyler Luu5861a9a2011-10-06 00:00:03 -0500113 return rc;
114 }
115 initHalPreviewWindow();
116 return rc;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700117 }
118
119 /** Set the ANativeWindow to which preview frames are sent */
120 status_t setPreviewWindow(const sp<ANativeWindow>& buf)
121 {
Steve Block3856b092011-10-20 11:56:00 +0100122 ALOGV("%s(%s) buf %p", __FUNCTION__, mName.string(), buf.get());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700123
124 if (mDevice->ops->set_preview_window) {
125 mPreviewWindow = buf;
126 mHalPreviewWindow.user = this;
Steve Block3856b092011-10-20 11:56:00 +0100127 ALOGV("%s &mHalPreviewWindow %p mHalPreviewWindow.user %p", __FUNCTION__,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700128 &mHalPreviewWindow, mHalPreviewWindow.user);
129 return mDevice->ops->set_preview_window(mDevice,
130 buf.get() ? &mHalPreviewWindow.nw : 0);
131 }
132 return INVALID_OPERATION;
133 }
134
135 /** Set the notification and data callbacks */
136 void setCallbacks(notify_callback notify_cb,
137 data_callback data_cb,
138 data_callback_timestamp data_cb_timestamp,
139 void* user)
140 {
141 mNotifyCb = notify_cb;
142 mDataCb = data_cb;
143 mDataCbTimestamp = data_cb_timestamp;
144 mCbUser = user;
145
Steve Block3856b092011-10-20 11:56:00 +0100146 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700147
148 if (mDevice->ops->set_callbacks) {
149 mDevice->ops->set_callbacks(mDevice,
150 __notify_cb,
151 __data_cb,
152 __data_cb_timestamp,
153 __get_memory,
154 this);
155 }
156 }
157
158 /**
159 * The following three functions all take a msgtype,
160 * which is a bitmask of the messages defined in
161 * include/ui/Camera.h
162 */
163
164 /**
165 * Enable a message, or set of messages.
166 */
167 void enableMsgType(int32_t msgType)
168 {
Steve Block3856b092011-10-20 11:56:00 +0100169 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700170 if (mDevice->ops->enable_msg_type)
171 mDevice->ops->enable_msg_type(mDevice, msgType);
172 }
173
174 /**
175 * Disable a message, or a set of messages.
176 *
177 * Once received a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), camera hal
178 * should not rely on its client to call releaseRecordingFrame() to release
179 * video recording frames sent out by the cameral hal before and after the
180 * disableMsgType(CAMERA_MSG_VIDEO_FRAME) call. Camera hal clients must not
181 * modify/access any video recording frame after calling
182 * disableMsgType(CAMERA_MSG_VIDEO_FRAME).
183 */
184 void disableMsgType(int32_t msgType)
185 {
Steve Block3856b092011-10-20 11:56:00 +0100186 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700187 if (mDevice->ops->disable_msg_type)
188 mDevice->ops->disable_msg_type(mDevice, msgType);
189 }
190
191 /**
192 * Query whether a message, or a set of messages, is enabled.
193 * Note that this is operates as an AND, if any of the messages
194 * queried are off, this will return false.
195 */
196 int msgTypeEnabled(int32_t msgType)
197 {
Steve Block3856b092011-10-20 11:56:00 +0100198 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700199 if (mDevice->ops->msg_type_enabled)
200 return mDevice->ops->msg_type_enabled(mDevice, msgType);
201 return false;
202 }
203
204 /**
205 * Start preview mode.
206 */
207 status_t startPreview()
208 {
Steve Block3856b092011-10-20 11:56:00 +0100209 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700210 if (mDevice->ops->start_preview)
211 return mDevice->ops->start_preview(mDevice);
212 return INVALID_OPERATION;
213 }
214
215 /**
216 * Stop a previously started preview.
217 */
218 void stopPreview()
219 {
Steve Block3856b092011-10-20 11:56:00 +0100220 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700221 if (mDevice->ops->stop_preview)
222 mDevice->ops->stop_preview(mDevice);
223 }
224
225 /**
226 * Returns true if preview is enabled.
227 */
228 int previewEnabled()
229 {
Steve Block3856b092011-10-20 11:56:00 +0100230 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700231 if (mDevice->ops->preview_enabled)
232 return mDevice->ops->preview_enabled(mDevice);
233 return false;
234 }
235
236 /**
237 * Request the camera hal to store meta data or real YUV data in
238 * the video buffers send out via CAMERA_MSG_VIDEO_FRRAME for a
239 * recording session. If it is not called, the default camera
240 * hal behavior is to store real YUV data in the video buffers.
241 *
242 * This method should be called before startRecording() in order
243 * to be effective.
244 *
245 * If meta data is stored in the video buffers, it is up to the
246 * receiver of the video buffers to interpret the contents and
247 * to find the actual frame data with the help of the meta data
248 * in the buffer. How this is done is outside of the scope of
249 * this method.
250 *
251 * Some camera hal may not support storing meta data in the video
252 * buffers, but all camera hal should support storing real YUV data
253 * in the video buffers. If the camera hal does not support storing
254 * the meta data in the video buffers when it is requested to do
255 * do, INVALID_OPERATION must be returned. It is very useful for
256 * the camera hal to pass meta data rather than the actual frame
257 * data directly to the video encoder, since the amount of the
258 * uncompressed frame data can be very large if video size is large.
259 *
260 * @param enable if true to instruct the camera hal to store
261 * meta data in the video buffers; false to instruct
262 * the camera hal to store real YUV data in the video
263 * buffers.
264 *
265 * @return OK on success.
266 */
267
268 status_t storeMetaDataInBuffers(int enable)
269 {
Steve Block3856b092011-10-20 11:56:00 +0100270 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700271 if (mDevice->ops->store_meta_data_in_buffers)
272 return mDevice->ops->store_meta_data_in_buffers(mDevice, enable);
273 return enable ? INVALID_OPERATION: OK;
274 }
275
276 /**
277 * Start record mode. When a record image is available a CAMERA_MSG_VIDEO_FRAME
278 * message is sent with the corresponding frame. Every record frame must be released
279 * by a cameral hal client via releaseRecordingFrame() before the client calls
280 * disableMsgType(CAMERA_MSG_VIDEO_FRAME). After the client calls
281 * disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's responsibility
282 * to manage the life-cycle of the video recording frames, and the client must
283 * not modify/access any video recording frames.
284 */
285 status_t startRecording()
286 {
Steve Block3856b092011-10-20 11:56:00 +0100287 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700288 if (mDevice->ops->start_recording)
289 return mDevice->ops->start_recording(mDevice);
290 return INVALID_OPERATION;
291 }
292
293 /**
294 * Stop a previously started recording.
295 */
296 void stopRecording()
297 {
Steve Block3856b092011-10-20 11:56:00 +0100298 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700299 if (mDevice->ops->stop_recording)
300 mDevice->ops->stop_recording(mDevice);
301 }
302
303 /**
304 * Returns true if recording is enabled.
305 */
306 int recordingEnabled()
307 {
Steve Block3856b092011-10-20 11:56:00 +0100308 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700309 if (mDevice->ops->recording_enabled)
310 return mDevice->ops->recording_enabled(mDevice);
311 return false;
312 }
313
314 /**
315 * Release a record frame previously returned by CAMERA_MSG_VIDEO_FRAME.
316 *
317 * It is camera hal client's responsibility to release video recording
318 * frames sent out by the camera hal before the camera hal receives
319 * a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME). After it receives
320 * the call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's
321 * responsibility of managing the life-cycle of the video recording
322 * frames.
323 */
324 void releaseRecordingFrame(const sp<IMemory>& mem)
325 {
Steve Block3856b092011-10-20 11:56:00 +0100326 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700327 if (mDevice->ops->release_recording_frame) {
328 ssize_t offset;
329 size_t size;
330 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
331 void *data = ((uint8_t *)heap->base()) + offset;
332 return mDevice->ops->release_recording_frame(mDevice, data);
333 }
334 }
335
336 /**
337 * Start auto focus, the notification callback routine is called
338 * with CAMERA_MSG_FOCUS once when focusing is complete. autoFocus()
339 * will be called again if another auto focus is needed.
340 */
341 status_t autoFocus()
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->auto_focus)
345 return mDevice->ops->auto_focus(mDevice);
346 return INVALID_OPERATION;
347 }
348
349 /**
350 * Cancels auto-focus function. If the auto-focus is still in progress,
351 * this function will cancel it. Whether the auto-focus is in progress
352 * or not, this function will return the focus position to the default.
353 * If the camera does not support auto-focus, this is a no-op.
354 */
355 status_t cancelAutoFocus()
356 {
Steve Block3856b092011-10-20 11:56:00 +0100357 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700358 if (mDevice->ops->cancel_auto_focus)
359 return mDevice->ops->cancel_auto_focus(mDevice);
360 return INVALID_OPERATION;
361 }
362
363 /**
364 * Take a picture.
365 */
366 status_t takePicture()
367 {
Steve Block3856b092011-10-20 11:56:00 +0100368 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700369 if (mDevice->ops->take_picture)
370 return mDevice->ops->take_picture(mDevice);
371 return INVALID_OPERATION;
372 }
373
374 /**
375 * Cancel a picture that was started with takePicture. Calling this
376 * method when no picture is being taken is a no-op.
377 */
378 status_t cancelPicture()
379 {
Steve Block3856b092011-10-20 11:56:00 +0100380 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700381 if (mDevice->ops->cancel_picture)
382 return mDevice->ops->cancel_picture(mDevice);
383 return INVALID_OPERATION;
384 }
385
386 /**
387 * Set the camera parameters. This returns BAD_VALUE if any parameter is
388 * invalid or not supported. */
389 status_t setParameters(const CameraParameters &params)
390 {
Steve Block3856b092011-10-20 11:56:00 +0100391 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700392 if (mDevice->ops->set_parameters)
393 return mDevice->ops->set_parameters(mDevice,
394 params.flatten().string());
395 return INVALID_OPERATION;
396 }
397
398 /** Return the camera parameters. */
399 CameraParameters getParameters() const
400 {
Steve Block3856b092011-10-20 11:56:00 +0100401 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700402 CameraParameters parms;
403 if (mDevice->ops->get_parameters) {
404 char *temp = mDevice->ops->get_parameters(mDevice);
405 String8 str_parms(temp);
Iliyan Malchev85fb61e2011-07-26 15:56:44 -0700406 if (mDevice->ops->put_parameters)
407 mDevice->ops->put_parameters(mDevice, temp);
408 else
409 free(temp);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700410 parms.unflatten(str_parms);
411 }
412 return parms;
413 }
414
415 /**
416 * Send command to camera driver.
417 */
418 status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
419 {
Steve Block3856b092011-10-20 11:56:00 +0100420 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700421 if (mDevice->ops->send_command)
422 return mDevice->ops->send_command(mDevice, cmd, arg1, arg2);
423 return INVALID_OPERATION;
424 }
425
426 /**
427 * Release the hardware resources owned by this object. Note that this is
428 * *not* done in the destructor.
429 */
430 void release() {
Steve Block3856b092011-10-20 11:56:00 +0100431 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700432 if (mDevice->ops->release)
433 mDevice->ops->release(mDevice);
434 }
435
436 /**
437 * Dump state of the camera hardware
438 */
Igor Murashkinddf3c502012-10-12 16:56:11 -0700439 status_t dump(int fd, const Vector<String16>& /*args*/) const
Iliyan Malchev8951a972011-04-14 16:55:59 -0700440 {
Steve Block3856b092011-10-20 11:56:00 +0100441 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700442 if (mDevice->ops->dump)
443 return mDevice->ops->dump(mDevice, fd);
444 return OK; // It's fine if the HAL doesn't implement dump()
445 }
446
447private:
448 camera_device_t *mDevice;
449 String8 mName;
450
451 static void __notify_cb(int32_t msg_type, int32_t ext1,
452 int32_t ext2, void *user)
453 {
Steve Block3856b092011-10-20 11:56:00 +0100454 ALOGV("%s", __FUNCTION__);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700455 CameraHardwareInterface *__this =
456 static_cast<CameraHardwareInterface *>(user);
457 __this->mNotifyCb(msg_type, ext1, ext2, __this->mCbUser);
458 }
459
460 static void __data_cb(int32_t msg_type,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700461 const camera_memory_t *data, unsigned int index,
Wu-cheng Liff09ef82011-07-28 05:30:59 +0800462 camera_frame_metadata_t *metadata,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700463 void *user)
464 {
Steve Block3856b092011-10-20 11:56:00 +0100465 ALOGV("%s", __FUNCTION__);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700466 CameraHardwareInterface *__this =
467 static_cast<CameraHardwareInterface *>(user);
468 sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(data->handle));
Iliyan Malchev26adde82011-06-06 17:21:32 -0700469 if (index >= mem->mNumBufs) {
Steve Block29357bc2012-01-06 19:20:56 +0000470 ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700471 index, mem->mNumBufs);
472 return;
473 }
Wu-cheng Liff09ef82011-07-28 05:30:59 +0800474 __this->mDataCb(msg_type, mem->mBuffers[index], metadata, __this->mCbUser);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700475 }
476
477 static void __data_cb_timestamp(nsecs_t timestamp, int32_t msg_type,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700478 const camera_memory_t *data, unsigned index,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700479 void *user)
480 {
Steve Block3856b092011-10-20 11:56:00 +0100481 ALOGV("%s", __FUNCTION__);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700482 CameraHardwareInterface *__this =
483 static_cast<CameraHardwareInterface *>(user);
484 // Start refcounting the heap object from here on. When the clients
485 // drop all references, it will be destroyed (as well as the enclosed
486 // MemoryHeapBase.
487 sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(data->handle));
Iliyan Malchev26adde82011-06-06 17:21:32 -0700488 if (index >= mem->mNumBufs) {
Steve Block29357bc2012-01-06 19:20:56 +0000489 ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700490 index, mem->mNumBufs);
491 return;
492 }
493 __this->mDataCbTimestamp(timestamp, msg_type, mem->mBuffers[index], __this->mCbUser);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700494 }
495
496 // This is a utility class that combines a MemoryHeapBase and a MemoryBase
497 // in one. Since we tend to use them in a one-to-one relationship, this is
498 // handy.
499
Iliyan Malchev26adde82011-06-06 17:21:32 -0700500 class CameraHeapMemory : public RefBase {
Iliyan Malchev8951a972011-04-14 16:55:59 -0700501 public:
Iliyan Malchev26adde82011-06-06 17:21:32 -0700502 CameraHeapMemory(int fd, size_t buf_size, uint_t num_buffers = 1) :
503 mBufSize(buf_size),
504 mNumBufs(num_buffers)
Iliyan Malchev8951a972011-04-14 16:55:59 -0700505 {
Iliyan Malchev26adde82011-06-06 17:21:32 -0700506 mHeap = new MemoryHeapBase(fd, buf_size * num_buffers);
507 commonInitialization();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700508 }
509
Iliyan Malchev26adde82011-06-06 17:21:32 -0700510 CameraHeapMemory(size_t buf_size, uint_t num_buffers = 1) :
511 mBufSize(buf_size),
512 mNumBufs(num_buffers)
513 {
514 mHeap = new MemoryHeapBase(buf_size * num_buffers);
515 commonInitialization();
516 }
517
518 void commonInitialization()
519 {
520 handle.data = mHeap->base();
521 handle.size = mBufSize * mNumBufs;
522 handle.handle = this;
523
524 mBuffers = new sp<MemoryBase>[mNumBufs];
525 for (uint_t i = 0; i < mNumBufs; i++)
526 mBuffers[i] = new MemoryBase(mHeap,
527 i * mBufSize,
528 mBufSize);
529
530 handle.release = __put_memory;
531 }
532
533 virtual ~CameraHeapMemory()
534 {
535 delete [] mBuffers;
536 }
537
538 size_t mBufSize;
539 uint_t mNumBufs;
540 sp<MemoryHeapBase> mHeap;
541 sp<MemoryBase> *mBuffers;
542
Iliyan Malchev8951a972011-04-14 16:55:59 -0700543 camera_memory_t handle;
544 };
545
Iliyan Malchev26adde82011-06-06 17:21:32 -0700546 static camera_memory_t* __get_memory(int fd, size_t buf_size, uint_t num_bufs,
547 void *user __attribute__((unused)))
Iliyan Malchev8951a972011-04-14 16:55:59 -0700548 {
Iliyan Malchev26adde82011-06-06 17:21:32 -0700549 CameraHeapMemory *mem;
550 if (fd < 0)
551 mem = new CameraHeapMemory(buf_size, num_bufs);
552 else
553 mem = new CameraHeapMemory(fd, buf_size, num_bufs);
554 mem->incStrong(mem);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700555 return &mem->handle;
556 }
557
Iliyan Malchev26adde82011-06-06 17:21:32 -0700558 static void __put_memory(camera_memory_t *data)
559 {
560 if (!data)
561 return;
562
563 CameraHeapMemory *mem = static_cast<CameraHeapMemory *>(data->handle);
564 mem->decStrong(mem);
565 }
566
Iliyan Malchev8951a972011-04-14 16:55:59 -0700567 static ANativeWindow *__to_anw(void *user)
568 {
569 CameraHardwareInterface *__this =
570 reinterpret_cast<CameraHardwareInterface *>(user);
571 return __this->mPreviewWindow.get();
572 }
573#define anw(n) __to_anw(((struct camera_preview_window *)n)->user)
574
575 static int __dequeue_buffer(struct preview_stream_ops* w,
Iliyan Malchevafcedc92011-06-10 16:05:23 -0700576 buffer_handle_t** buffer, int *stride)
Iliyan Malchev8951a972011-04-14 16:55:59 -0700577 {
578 int rc;
579 ANativeWindow *a = anw(w);
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700580 ANativeWindowBuffer* anb;
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700581 rc = native_window_dequeue_buffer_and_wait(a, &anb);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700582 if (!rc) {
Sundar Raman1e06f432011-06-17 09:05:09 -0500583 *buffer = &anb->handle;
584 *stride = anb->stride;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700585 }
586 return rc;
587 }
588
589#ifndef container_of
590#define container_of(ptr, type, member) ({ \
591 const typeof(((type *) 0)->member) *__mptr = (ptr); \
592 (type *) ((char *) __mptr - (char *)(&((type *)0)->member)); })
593#endif
594
Sundar Raman1e06f432011-06-17 09:05:09 -0500595 static int __lock_buffer(struct preview_stream_ops* w,
Igor Murashkinddf3c502012-10-12 16:56:11 -0700596 buffer_handle_t* /*buffer*/)
Sundar Raman1e06f432011-06-17 09:05:09 -0500597 {
598 ANativeWindow *a = anw(w);
Igor Murashkinddf3c502012-10-12 16:56:11 -0700599 (void)a;
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700600 return 0;
Sundar Raman1e06f432011-06-17 09:05:09 -0500601 }
602
Iliyan Malchev8951a972011-04-14 16:55:59 -0700603 static int __enqueue_buffer(struct preview_stream_ops* w,
604 buffer_handle_t* buffer)
605 {
606 ANativeWindow *a = anw(w);
607 return a->queueBuffer(a,
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700608 container_of(buffer, ANativeWindowBuffer, handle), -1);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700609 }
610
611 static int __cancel_buffer(struct preview_stream_ops* w,
612 buffer_handle_t* buffer)
613 {
614 ANativeWindow *a = anw(w);
615 return a->cancelBuffer(a,
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700616 container_of(buffer, ANativeWindowBuffer, handle), -1);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700617 }
618
619 static int __set_buffer_count(struct preview_stream_ops* w, int count)
620 {
621 ANativeWindow *a = anw(w);
Iliyan Malchev26adde82011-06-06 17:21:32 -0700622 return native_window_set_buffer_count(a, count);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700623 }
624
625 static int __set_buffers_geometry(struct preview_stream_ops* w,
626 int width, int height, int format)
627 {
Pierre Couillaudc09cec72014-07-03 10:55:00 -0700628 int rc;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700629 ANativeWindow *a = anw(w);
Pierre Couillaudc09cec72014-07-03 10:55:00 -0700630
631 rc = native_window_set_buffers_dimensions(a, width, height);
632 if (!rc) {
633 rc = native_window_set_buffers_format(a, format);
634 }
635 return rc;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700636 }
637
638 static int __set_crop(struct preview_stream_ops *w,
639 int left, int top, int right, int bottom)
640 {
641 ANativeWindow *a = anw(w);
642 android_native_rect_t crop;
643 crop.left = left;
644 crop.top = top;
645 crop.right = right;
646 crop.bottom = bottom;
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700647 return native_window_set_crop(a, &crop);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700648 }
649
Eino-Ville Talvala9f3d5912011-07-26 14:06:07 -0700650 static int __set_timestamp(struct preview_stream_ops *w,
651 int64_t timestamp) {
652 ANativeWindow *a = anw(w);
653 return native_window_set_buffers_timestamp(a, timestamp);
654 }
655
Iliyan Malchev8951a972011-04-14 16:55:59 -0700656 static int __set_usage(struct preview_stream_ops* w, int usage)
657 {
658 ANativeWindow *a = anw(w);
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700659 return native_window_set_usage(a, usage);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700660 }
661
662 static int __set_swap_interval(struct preview_stream_ops *w, int interval)
663 {
664 ANativeWindow *a = anw(w);
665 return a->setSwapInterval(a, interval);
666 }
667
668 static int __get_min_undequeued_buffer_count(
669 const struct preview_stream_ops *w,
670 int *count)
671 {
672 ANativeWindow *a = anw(w);
673 return a->query(a, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, count);
674 }
675
676 void initHalPreviewWindow()
677 {
678 mHalPreviewWindow.nw.cancel_buffer = __cancel_buffer;
Sundar Raman1e06f432011-06-17 09:05:09 -0500679 mHalPreviewWindow.nw.lock_buffer = __lock_buffer;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700680 mHalPreviewWindow.nw.dequeue_buffer = __dequeue_buffer;
681 mHalPreviewWindow.nw.enqueue_buffer = __enqueue_buffer;
682 mHalPreviewWindow.nw.set_buffer_count = __set_buffer_count;
683 mHalPreviewWindow.nw.set_buffers_geometry = __set_buffers_geometry;
684 mHalPreviewWindow.nw.set_crop = __set_crop;
Eino-Ville Talvala9f3d5912011-07-26 14:06:07 -0700685 mHalPreviewWindow.nw.set_timestamp = __set_timestamp;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700686 mHalPreviewWindow.nw.set_usage = __set_usage;
687 mHalPreviewWindow.nw.set_swap_interval = __set_swap_interval;
688
689 mHalPreviewWindow.nw.get_min_undequeued_buffer_count =
690 __get_min_undequeued_buffer_count;
691 }
692
693 sp<ANativeWindow> mPreviewWindow;
694
695 struct camera_preview_window {
696 struct preview_stream_ops nw;
697 void *user;
698 };
699
700 struct camera_preview_window mHalPreviewWindow;
701
702 notify_callback mNotifyCb;
703 data_callback mDataCb;
704 data_callback_timestamp mDataCbTimestamp;
705 void *mCbUser;
706};
707
708}; // namespace android
709
710#endif