blob: 454d60f2456d537fd29ad096d3b106f53cb88332 [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
Yin-Chia Yehe074a932015-01-30 10:29:02 -080092 status_t initialize(CameraModule *module)
Tyler Luu5861a9a2011-10-06 00:00:03 -050093 {
Steve Blockdf64d152012-01-04 20:05:49 +000094 ALOGI("Opening camera %s", mName.string());
Zhijun Heb10cdad2014-06-16 16:38:35 -070095 camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -080096 status_t res = module->getCameraInfo(atoi(mName.string()), &info);
Zhijun Heb10cdad2014-06-16 16:38:35 -070097 if (res != OK) return res;
98
99 int rc = OK;
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800100 if (module->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_3 &&
Zhijun Heb10cdad2014-06-16 16:38:35 -0700101 info.device_version > CAMERA_DEVICE_API_VERSION_1_0) {
102 // Open higher version camera device as HAL1.0 device.
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800103 rc = module->openLegacy(mName.string(),
104 CAMERA_DEVICE_API_VERSION_1_0,
105 (hw_device_t **)&mDevice);
Zhijun Heb10cdad2014-06-16 16:38:35 -0700106 } else {
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800107 rc = module->open(mName.string(), (hw_device_t **)&mDevice);
Zhijun Heb10cdad2014-06-16 16:38:35 -0700108 }
Tyler Luu5861a9a2011-10-06 00:00:03 -0500109 if (rc != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000110 ALOGE("Could not open camera %s: %d", mName.string(), rc);
Tyler Luu5861a9a2011-10-06 00:00:03 -0500111 return rc;
112 }
113 initHalPreviewWindow();
114 return rc;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700115 }
116
117 /** Set the ANativeWindow to which preview frames are sent */
118 status_t setPreviewWindow(const sp<ANativeWindow>& buf)
119 {
Steve Block3856b092011-10-20 11:56:00 +0100120 ALOGV("%s(%s) buf %p", __FUNCTION__, mName.string(), buf.get());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700121
122 if (mDevice->ops->set_preview_window) {
123 mPreviewWindow = buf;
124 mHalPreviewWindow.user = this;
Steve Block3856b092011-10-20 11:56:00 +0100125 ALOGV("%s &mHalPreviewWindow %p mHalPreviewWindow.user %p", __FUNCTION__,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700126 &mHalPreviewWindow, mHalPreviewWindow.user);
127 return mDevice->ops->set_preview_window(mDevice,
128 buf.get() ? &mHalPreviewWindow.nw : 0);
129 }
130 return INVALID_OPERATION;
131 }
132
133 /** Set the notification and data callbacks */
134 void setCallbacks(notify_callback notify_cb,
135 data_callback data_cb,
136 data_callback_timestamp data_cb_timestamp,
137 void* user)
138 {
139 mNotifyCb = notify_cb;
140 mDataCb = data_cb;
141 mDataCbTimestamp = data_cb_timestamp;
142 mCbUser = user;
143
Steve Block3856b092011-10-20 11:56:00 +0100144 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700145
146 if (mDevice->ops->set_callbacks) {
147 mDevice->ops->set_callbacks(mDevice,
148 __notify_cb,
149 __data_cb,
150 __data_cb_timestamp,
151 __get_memory,
152 this);
153 }
154 }
155
156 /**
157 * The following three functions all take a msgtype,
158 * which is a bitmask of the messages defined in
159 * include/ui/Camera.h
160 */
161
162 /**
163 * Enable a message, or set of messages.
164 */
165 void enableMsgType(int32_t msgType)
166 {
Steve Block3856b092011-10-20 11:56:00 +0100167 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700168 if (mDevice->ops->enable_msg_type)
169 mDevice->ops->enable_msg_type(mDevice, msgType);
170 }
171
172 /**
173 * Disable a message, or a set of messages.
174 *
175 * Once received a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), camera hal
176 * should not rely on its client to call releaseRecordingFrame() to release
177 * video recording frames sent out by the cameral hal before and after the
178 * disableMsgType(CAMERA_MSG_VIDEO_FRAME) call. Camera hal clients must not
179 * modify/access any video recording frame after calling
180 * disableMsgType(CAMERA_MSG_VIDEO_FRAME).
181 */
182 void disableMsgType(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->disable_msg_type)
186 mDevice->ops->disable_msg_type(mDevice, msgType);
187 }
188
189 /**
190 * Query whether a message, or a set of messages, is enabled.
191 * Note that this is operates as an AND, if any of the messages
192 * queried are off, this will return false.
193 */
194 int msgTypeEnabled(int32_t msgType)
195 {
Steve Block3856b092011-10-20 11:56:00 +0100196 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700197 if (mDevice->ops->msg_type_enabled)
198 return mDevice->ops->msg_type_enabled(mDevice, msgType);
199 return false;
200 }
201
202 /**
203 * Start preview mode.
204 */
205 status_t startPreview()
206 {
Steve Block3856b092011-10-20 11:56:00 +0100207 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700208 if (mDevice->ops->start_preview)
209 return mDevice->ops->start_preview(mDevice);
210 return INVALID_OPERATION;
211 }
212
213 /**
214 * Stop a previously started preview.
215 */
216 void stopPreview()
217 {
Steve Block3856b092011-10-20 11:56:00 +0100218 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700219 if (mDevice->ops->stop_preview)
220 mDevice->ops->stop_preview(mDevice);
221 }
222
223 /**
224 * Returns true if preview is enabled.
225 */
226 int previewEnabled()
227 {
Steve Block3856b092011-10-20 11:56:00 +0100228 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700229 if (mDevice->ops->preview_enabled)
230 return mDevice->ops->preview_enabled(mDevice);
231 return false;
232 }
233
234 /**
235 * Request the camera hal to store meta data or real YUV data in
236 * the video buffers send out via CAMERA_MSG_VIDEO_FRRAME for a
237 * recording session. If it is not called, the default camera
238 * hal behavior is to store real YUV data in the video buffers.
239 *
240 * This method should be called before startRecording() in order
241 * to be effective.
242 *
243 * If meta data is stored in the video buffers, it is up to the
244 * receiver of the video buffers to interpret the contents and
245 * to find the actual frame data with the help of the meta data
246 * in the buffer. How this is done is outside of the scope of
247 * this method.
248 *
249 * Some camera hal may not support storing meta data in the video
250 * buffers, but all camera hal should support storing real YUV data
251 * in the video buffers. If the camera hal does not support storing
252 * the meta data in the video buffers when it is requested to do
253 * do, INVALID_OPERATION must be returned. It is very useful for
254 * the camera hal to pass meta data rather than the actual frame
255 * data directly to the video encoder, since the amount of the
256 * uncompressed frame data can be very large if video size is large.
257 *
258 * @param enable if true to instruct the camera hal to store
259 * meta data in the video buffers; false to instruct
260 * the camera hal to store real YUV data in the video
261 * buffers.
262 *
263 * @return OK on success.
264 */
265
266 status_t storeMetaDataInBuffers(int enable)
267 {
Steve Block3856b092011-10-20 11:56:00 +0100268 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700269 if (mDevice->ops->store_meta_data_in_buffers)
270 return mDevice->ops->store_meta_data_in_buffers(mDevice, enable);
271 return enable ? INVALID_OPERATION: OK;
272 }
273
274 /**
275 * Start record mode. When a record image is available a CAMERA_MSG_VIDEO_FRAME
276 * message is sent with the corresponding frame. Every record frame must be released
277 * by a cameral hal client via releaseRecordingFrame() before the client calls
278 * disableMsgType(CAMERA_MSG_VIDEO_FRAME). After the client calls
279 * disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's responsibility
280 * to manage the life-cycle of the video recording frames, and the client must
281 * not modify/access any video recording frames.
282 */
283 status_t startRecording()
284 {
Steve Block3856b092011-10-20 11:56:00 +0100285 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700286 if (mDevice->ops->start_recording)
287 return mDevice->ops->start_recording(mDevice);
288 return INVALID_OPERATION;
289 }
290
291 /**
292 * Stop a previously started recording.
293 */
294 void stopRecording()
295 {
Steve Block3856b092011-10-20 11:56:00 +0100296 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700297 if (mDevice->ops->stop_recording)
298 mDevice->ops->stop_recording(mDevice);
299 }
300
301 /**
302 * Returns true if recording is enabled.
303 */
304 int recordingEnabled()
305 {
Steve Block3856b092011-10-20 11:56:00 +0100306 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700307 if (mDevice->ops->recording_enabled)
308 return mDevice->ops->recording_enabled(mDevice);
309 return false;
310 }
311
312 /**
313 * Release a record frame previously returned by CAMERA_MSG_VIDEO_FRAME.
314 *
315 * It is camera hal client's responsibility to release video recording
316 * frames sent out by the camera hal before the camera hal receives
317 * a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME). After it receives
318 * the call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's
319 * responsibility of managing the life-cycle of the video recording
320 * frames.
321 */
322 void releaseRecordingFrame(const sp<IMemory>& mem)
323 {
Steve Block3856b092011-10-20 11:56:00 +0100324 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700325 if (mDevice->ops->release_recording_frame) {
326 ssize_t offset;
327 size_t size;
328 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
329 void *data = ((uint8_t *)heap->base()) + offset;
330 return mDevice->ops->release_recording_frame(mDevice, data);
331 }
332 }
333
334 /**
335 * Start auto focus, the notification callback routine is called
336 * with CAMERA_MSG_FOCUS once when focusing is complete. autoFocus()
337 * will be called again if another auto focus is needed.
338 */
339 status_t autoFocus()
340 {
Steve Block3856b092011-10-20 11:56:00 +0100341 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700342 if (mDevice->ops->auto_focus)
343 return mDevice->ops->auto_focus(mDevice);
344 return INVALID_OPERATION;
345 }
346
347 /**
348 * Cancels auto-focus function. If the auto-focus is still in progress,
349 * this function will cancel it. Whether the auto-focus is in progress
350 * or not, this function will return the focus position to the default.
351 * If the camera does not support auto-focus, this is a no-op.
352 */
353 status_t cancelAutoFocus()
354 {
Steve Block3856b092011-10-20 11:56:00 +0100355 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700356 if (mDevice->ops->cancel_auto_focus)
357 return mDevice->ops->cancel_auto_focus(mDevice);
358 return INVALID_OPERATION;
359 }
360
361 /**
362 * Take a picture.
363 */
364 status_t takePicture()
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->take_picture)
368 return mDevice->ops->take_picture(mDevice);
369 return INVALID_OPERATION;
370 }
371
372 /**
373 * Cancel a picture that was started with takePicture. Calling this
374 * method when no picture is being taken is a no-op.
375 */
376 status_t cancelPicture()
377 {
Steve Block3856b092011-10-20 11:56:00 +0100378 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700379 if (mDevice->ops->cancel_picture)
380 return mDevice->ops->cancel_picture(mDevice);
381 return INVALID_OPERATION;
382 }
383
384 /**
385 * Set the camera parameters. This returns BAD_VALUE if any parameter is
386 * invalid or not supported. */
387 status_t setParameters(const CameraParameters &params)
388 {
Steve Block3856b092011-10-20 11:56:00 +0100389 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700390 if (mDevice->ops->set_parameters)
391 return mDevice->ops->set_parameters(mDevice,
392 params.flatten().string());
393 return INVALID_OPERATION;
394 }
395
396 /** Return the camera parameters. */
397 CameraParameters getParameters() const
398 {
Steve Block3856b092011-10-20 11:56:00 +0100399 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700400 CameraParameters parms;
401 if (mDevice->ops->get_parameters) {
402 char *temp = mDevice->ops->get_parameters(mDevice);
403 String8 str_parms(temp);
Iliyan Malchev85fb61e2011-07-26 15:56:44 -0700404 if (mDevice->ops->put_parameters)
405 mDevice->ops->put_parameters(mDevice, temp);
406 else
407 free(temp);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700408 parms.unflatten(str_parms);
409 }
410 return parms;
411 }
412
413 /**
414 * Send command to camera driver.
415 */
416 status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
417 {
Steve Block3856b092011-10-20 11:56:00 +0100418 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700419 if (mDevice->ops->send_command)
420 return mDevice->ops->send_command(mDevice, cmd, arg1, arg2);
421 return INVALID_OPERATION;
422 }
423
424 /**
425 * Release the hardware resources owned by this object. Note that this is
426 * *not* done in the destructor.
427 */
428 void release() {
Steve Block3856b092011-10-20 11:56:00 +0100429 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700430 if (mDevice->ops->release)
431 mDevice->ops->release(mDevice);
432 }
433
434 /**
435 * Dump state of the camera hardware
436 */
Igor Murashkinddf3c502012-10-12 16:56:11 -0700437 status_t dump(int fd, const Vector<String16>& /*args*/) const
Iliyan Malchev8951a972011-04-14 16:55:59 -0700438 {
Steve Block3856b092011-10-20 11:56:00 +0100439 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700440 if (mDevice->ops->dump)
441 return mDevice->ops->dump(mDevice, fd);
442 return OK; // It's fine if the HAL doesn't implement dump()
443 }
444
445private:
446 camera_device_t *mDevice;
447 String8 mName;
448
449 static void __notify_cb(int32_t msg_type, int32_t ext1,
450 int32_t ext2, void *user)
451 {
Steve Block3856b092011-10-20 11:56:00 +0100452 ALOGV("%s", __FUNCTION__);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700453 CameraHardwareInterface *__this =
454 static_cast<CameraHardwareInterface *>(user);
455 __this->mNotifyCb(msg_type, ext1, ext2, __this->mCbUser);
456 }
457
458 static void __data_cb(int32_t msg_type,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700459 const camera_memory_t *data, unsigned int index,
Wu-cheng Liff09ef82011-07-28 05:30:59 +0800460 camera_frame_metadata_t *metadata,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700461 void *user)
462 {
Steve Block3856b092011-10-20 11:56:00 +0100463 ALOGV("%s", __FUNCTION__);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700464 CameraHardwareInterface *__this =
465 static_cast<CameraHardwareInterface *>(user);
466 sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(data->handle));
Iliyan Malchev26adde82011-06-06 17:21:32 -0700467 if (index >= mem->mNumBufs) {
Steve Block29357bc2012-01-06 19:20:56 +0000468 ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700469 index, mem->mNumBufs);
470 return;
471 }
Wu-cheng Liff09ef82011-07-28 05:30:59 +0800472 __this->mDataCb(msg_type, mem->mBuffers[index], metadata, __this->mCbUser);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700473 }
474
475 static void __data_cb_timestamp(nsecs_t timestamp, int32_t msg_type,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700476 const camera_memory_t *data, unsigned index,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700477 void *user)
478 {
Steve Block3856b092011-10-20 11:56:00 +0100479 ALOGV("%s", __FUNCTION__);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700480 CameraHardwareInterface *__this =
481 static_cast<CameraHardwareInterface *>(user);
482 // Start refcounting the heap object from here on. When the clients
483 // drop all references, it will be destroyed (as well as the enclosed
484 // MemoryHeapBase.
485 sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(data->handle));
Iliyan Malchev26adde82011-06-06 17:21:32 -0700486 if (index >= mem->mNumBufs) {
Steve Block29357bc2012-01-06 19:20:56 +0000487 ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700488 index, mem->mNumBufs);
489 return;
490 }
491 __this->mDataCbTimestamp(timestamp, msg_type, mem->mBuffers[index], __this->mCbUser);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700492 }
493
494 // This is a utility class that combines a MemoryHeapBase and a MemoryBase
495 // in one. Since we tend to use them in a one-to-one relationship, this is
496 // handy.
497
Iliyan Malchev26adde82011-06-06 17:21:32 -0700498 class CameraHeapMemory : public RefBase {
Iliyan Malchev8951a972011-04-14 16:55:59 -0700499 public:
Iliyan Malchev26adde82011-06-06 17:21:32 -0700500 CameraHeapMemory(int fd, size_t buf_size, uint_t num_buffers = 1) :
501 mBufSize(buf_size),
502 mNumBufs(num_buffers)
Iliyan Malchev8951a972011-04-14 16:55:59 -0700503 {
Iliyan Malchev26adde82011-06-06 17:21:32 -0700504 mHeap = new MemoryHeapBase(fd, buf_size * num_buffers);
505 commonInitialization();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700506 }
507
Iliyan Malchev26adde82011-06-06 17:21:32 -0700508 CameraHeapMemory(size_t buf_size, uint_t num_buffers = 1) :
509 mBufSize(buf_size),
510 mNumBufs(num_buffers)
511 {
512 mHeap = new MemoryHeapBase(buf_size * num_buffers);
513 commonInitialization();
514 }
515
516 void commonInitialization()
517 {
518 handle.data = mHeap->base();
519 handle.size = mBufSize * mNumBufs;
520 handle.handle = this;
521
522 mBuffers = new sp<MemoryBase>[mNumBufs];
523 for (uint_t i = 0; i < mNumBufs; i++)
524 mBuffers[i] = new MemoryBase(mHeap,
525 i * mBufSize,
526 mBufSize);
527
528 handle.release = __put_memory;
529 }
530
531 virtual ~CameraHeapMemory()
532 {
533 delete [] mBuffers;
534 }
535
536 size_t mBufSize;
537 uint_t mNumBufs;
538 sp<MemoryHeapBase> mHeap;
539 sp<MemoryBase> *mBuffers;
540
Iliyan Malchev8951a972011-04-14 16:55:59 -0700541 camera_memory_t handle;
542 };
543
Iliyan Malchev26adde82011-06-06 17:21:32 -0700544 static camera_memory_t* __get_memory(int fd, size_t buf_size, uint_t num_bufs,
545 void *user __attribute__((unused)))
Iliyan Malchev8951a972011-04-14 16:55:59 -0700546 {
Iliyan Malchev26adde82011-06-06 17:21:32 -0700547 CameraHeapMemory *mem;
548 if (fd < 0)
549 mem = new CameraHeapMemory(buf_size, num_bufs);
550 else
551 mem = new CameraHeapMemory(fd, buf_size, num_bufs);
552 mem->incStrong(mem);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700553 return &mem->handle;
554 }
555
Iliyan Malchev26adde82011-06-06 17:21:32 -0700556 static void __put_memory(camera_memory_t *data)
557 {
558 if (!data)
559 return;
560
561 CameraHeapMemory *mem = static_cast<CameraHeapMemory *>(data->handle);
562 mem->decStrong(mem);
563 }
564
Iliyan Malchev8951a972011-04-14 16:55:59 -0700565 static ANativeWindow *__to_anw(void *user)
566 {
567 CameraHardwareInterface *__this =
568 reinterpret_cast<CameraHardwareInterface *>(user);
569 return __this->mPreviewWindow.get();
570 }
Chih-Hung Hsiehbf291732016-05-17 15:16:07 -0700571#define anw(n) __to_anw(((struct camera_preview_window *)(n))->user)
Iliyan Malchev8951a972011-04-14 16:55:59 -0700572
573 static int __dequeue_buffer(struct preview_stream_ops* w,
Iliyan Malchevafcedc92011-06-10 16:05:23 -0700574 buffer_handle_t** buffer, int *stride)
Iliyan Malchev8951a972011-04-14 16:55:59 -0700575 {
576 int rc;
577 ANativeWindow *a = anw(w);
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700578 ANativeWindowBuffer* anb;
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700579 rc = native_window_dequeue_buffer_and_wait(a, &anb);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700580 if (!rc) {
Sundar Raman1e06f432011-06-17 09:05:09 -0500581 *buffer = &anb->handle;
582 *stride = anb->stride;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700583 }
584 return rc;
585 }
586
587#ifndef container_of
588#define container_of(ptr, type, member) ({ \
Dan Albert36802bd2014-11-20 11:31:17 -0800589 const __typeof__(((type *) 0)->member) *__mptr = (ptr); \
Iliyan Malchev8951a972011-04-14 16:55:59 -0700590 (type *) ((char *) __mptr - (char *)(&((type *)0)->member)); })
591#endif
592
Sundar Raman1e06f432011-06-17 09:05:09 -0500593 static int __lock_buffer(struct preview_stream_ops* w,
Igor Murashkinddf3c502012-10-12 16:56:11 -0700594 buffer_handle_t* /*buffer*/)
Sundar Raman1e06f432011-06-17 09:05:09 -0500595 {
596 ANativeWindow *a = anw(w);
Igor Murashkinddf3c502012-10-12 16:56:11 -0700597 (void)a;
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700598 return 0;
Sundar Raman1e06f432011-06-17 09:05:09 -0500599 }
600
Iliyan Malchev8951a972011-04-14 16:55:59 -0700601 static int __enqueue_buffer(struct preview_stream_ops* w,
602 buffer_handle_t* buffer)
603 {
604 ANativeWindow *a = anw(w);
605 return a->queueBuffer(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 __cancel_buffer(struct preview_stream_ops* w,
610 buffer_handle_t* buffer)
611 {
612 ANativeWindow *a = anw(w);
613 return a->cancelBuffer(a,
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700614 container_of(buffer, ANativeWindowBuffer, handle), -1);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700615 }
616
617 static int __set_buffer_count(struct preview_stream_ops* w, int count)
618 {
619 ANativeWindow *a = anw(w);
Iliyan Malchev26adde82011-06-06 17:21:32 -0700620 return native_window_set_buffer_count(a, count);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700621 }
622
623 static int __set_buffers_geometry(struct preview_stream_ops* w,
624 int width, int height, int format)
625 {
Pierre Couillaudc09cec72014-07-03 10:55:00 -0700626 int rc;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700627 ANativeWindow *a = anw(w);
Pierre Couillaudc09cec72014-07-03 10:55:00 -0700628
629 rc = native_window_set_buffers_dimensions(a, width, height);
630 if (!rc) {
631 rc = native_window_set_buffers_format(a, format);
632 }
633 return rc;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700634 }
635
636 static int __set_crop(struct preview_stream_ops *w,
637 int left, int top, int right, int bottom)
638 {
639 ANativeWindow *a = anw(w);
640 android_native_rect_t crop;
641 crop.left = left;
642 crop.top = top;
643 crop.right = right;
644 crop.bottom = bottom;
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700645 return native_window_set_crop(a, &crop);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700646 }
647
Eino-Ville Talvala9f3d5912011-07-26 14:06:07 -0700648 static int __set_timestamp(struct preview_stream_ops *w,
649 int64_t timestamp) {
650 ANativeWindow *a = anw(w);
651 return native_window_set_buffers_timestamp(a, timestamp);
652 }
653
Iliyan Malchev8951a972011-04-14 16:55:59 -0700654 static int __set_usage(struct preview_stream_ops* w, int usage)
655 {
656 ANativeWindow *a = anw(w);
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700657 return native_window_set_usage(a, usage);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700658 }
659
660 static int __set_swap_interval(struct preview_stream_ops *w, int interval)
661 {
662 ANativeWindow *a = anw(w);
663 return a->setSwapInterval(a, interval);
664 }
665
666 static int __get_min_undequeued_buffer_count(
667 const struct preview_stream_ops *w,
668 int *count)
669 {
670 ANativeWindow *a = anw(w);
671 return a->query(a, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, count);
672 }
673
674 void initHalPreviewWindow()
675 {
676 mHalPreviewWindow.nw.cancel_buffer = __cancel_buffer;
Sundar Raman1e06f432011-06-17 09:05:09 -0500677 mHalPreviewWindow.nw.lock_buffer = __lock_buffer;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700678 mHalPreviewWindow.nw.dequeue_buffer = __dequeue_buffer;
679 mHalPreviewWindow.nw.enqueue_buffer = __enqueue_buffer;
680 mHalPreviewWindow.nw.set_buffer_count = __set_buffer_count;
681 mHalPreviewWindow.nw.set_buffers_geometry = __set_buffers_geometry;
682 mHalPreviewWindow.nw.set_crop = __set_crop;
Eino-Ville Talvala9f3d5912011-07-26 14:06:07 -0700683 mHalPreviewWindow.nw.set_timestamp = __set_timestamp;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700684 mHalPreviewWindow.nw.set_usage = __set_usage;
685 mHalPreviewWindow.nw.set_swap_interval = __set_swap_interval;
686
687 mHalPreviewWindow.nw.get_min_undequeued_buffer_count =
688 __get_min_undequeued_buffer_count;
689 }
690
691 sp<ANativeWindow> mPreviewWindow;
692
693 struct camera_preview_window {
694 struct preview_stream_ops nw;
695 void *user;
696 };
697
698 struct camera_preview_window mHalPreviewWindow;
699
700 notify_callback mNotifyCb;
701 data_callback mDataCb;
702 data_callback_timestamp mDataCbTimestamp;
703 void *mCbUser;
704};
705
706}; // namespace android
707
708#endif