blob: 9e1cdc92809d8bb6c60be69b85ffe296313db508 [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;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800100 if (module->getRawModule()->module_api_version >= 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 {
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800107 rc = CameraService::filterOpenErrorCode(module->open(
108 mName.string(), (hw_device_t **)&mDevice));
Zhijun Heb10cdad2014-06-16 16:38:35 -0700109 }
Tyler Luu5861a9a2011-10-06 00:00:03 -0500110 if (rc != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000111 ALOGE("Could not open camera %s: %d", mName.string(), rc);
Tyler Luu5861a9a2011-10-06 00:00:03 -0500112 return rc;
113 }
114 initHalPreviewWindow();
115 return rc;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700116 }
117
118 /** Set the ANativeWindow to which preview frames are sent */
119 status_t setPreviewWindow(const sp<ANativeWindow>& buf)
120 {
Steve Block3856b092011-10-20 11:56:00 +0100121 ALOGV("%s(%s) buf %p", __FUNCTION__, mName.string(), buf.get());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700122
123 if (mDevice->ops->set_preview_window) {
124 mPreviewWindow = buf;
125 mHalPreviewWindow.user = this;
Steve Block3856b092011-10-20 11:56:00 +0100126 ALOGV("%s &mHalPreviewWindow %p mHalPreviewWindow.user %p", __FUNCTION__,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700127 &mHalPreviewWindow, mHalPreviewWindow.user);
128 return mDevice->ops->set_preview_window(mDevice,
129 buf.get() ? &mHalPreviewWindow.nw : 0);
130 }
131 return INVALID_OPERATION;
132 }
133
134 /** Set the notification and data callbacks */
135 void setCallbacks(notify_callback notify_cb,
136 data_callback data_cb,
137 data_callback_timestamp data_cb_timestamp,
138 void* user)
139 {
140 mNotifyCb = notify_cb;
141 mDataCb = data_cb;
142 mDataCbTimestamp = data_cb_timestamp;
143 mCbUser = user;
144
Steve Block3856b092011-10-20 11:56:00 +0100145 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700146
147 if (mDevice->ops->set_callbacks) {
148 mDevice->ops->set_callbacks(mDevice,
149 __notify_cb,
150 __data_cb,
151 __data_cb_timestamp,
152 __get_memory,
153 this);
154 }
155 }
156
157 /**
158 * The following three functions all take a msgtype,
159 * which is a bitmask of the messages defined in
160 * include/ui/Camera.h
161 */
162
163 /**
164 * Enable a message, or set of messages.
165 */
166 void enableMsgType(int32_t msgType)
167 {
Steve Block3856b092011-10-20 11:56:00 +0100168 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700169 if (mDevice->ops->enable_msg_type)
170 mDevice->ops->enable_msg_type(mDevice, msgType);
171 }
172
173 /**
174 * Disable a message, or a set of messages.
175 *
176 * Once received a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), camera hal
177 * should not rely on its client to call releaseRecordingFrame() to release
178 * video recording frames sent out by the cameral hal before and after the
179 * disableMsgType(CAMERA_MSG_VIDEO_FRAME) call. Camera hal clients must not
180 * modify/access any video recording frame after calling
181 * disableMsgType(CAMERA_MSG_VIDEO_FRAME).
182 */
183 void disableMsgType(int32_t msgType)
184 {
Steve Block3856b092011-10-20 11:56:00 +0100185 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700186 if (mDevice->ops->disable_msg_type)
187 mDevice->ops->disable_msg_type(mDevice, msgType);
188 }
189
190 /**
191 * Query whether a message, or a set of messages, is enabled.
192 * Note that this is operates as an AND, if any of the messages
193 * queried are off, this will return false.
194 */
195 int msgTypeEnabled(int32_t msgType)
196 {
Steve Block3856b092011-10-20 11:56:00 +0100197 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700198 if (mDevice->ops->msg_type_enabled)
199 return mDevice->ops->msg_type_enabled(mDevice, msgType);
200 return false;
201 }
202
203 /**
204 * Start preview mode.
205 */
206 status_t startPreview()
207 {
Steve Block3856b092011-10-20 11:56:00 +0100208 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700209 if (mDevice->ops->start_preview)
210 return mDevice->ops->start_preview(mDevice);
211 return INVALID_OPERATION;
212 }
213
214 /**
215 * Stop a previously started preview.
216 */
217 void stopPreview()
218 {
Steve Block3856b092011-10-20 11:56:00 +0100219 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700220 if (mDevice->ops->stop_preview)
221 mDevice->ops->stop_preview(mDevice);
222 }
223
224 /**
225 * Returns true if preview is enabled.
226 */
227 int previewEnabled()
228 {
Steve Block3856b092011-10-20 11:56:00 +0100229 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700230 if (mDevice->ops->preview_enabled)
231 return mDevice->ops->preview_enabled(mDevice);
232 return false;
233 }
234
235 /**
236 * Request the camera hal to store meta data or real YUV data in
237 * the video buffers send out via CAMERA_MSG_VIDEO_FRRAME for a
238 * recording session. If it is not called, the default camera
239 * hal behavior is to store real YUV data in the video buffers.
240 *
241 * This method should be called before startRecording() in order
242 * to be effective.
243 *
244 * If meta data is stored in the video buffers, it is up to the
245 * receiver of the video buffers to interpret the contents and
246 * to find the actual frame data with the help of the meta data
247 * in the buffer. How this is done is outside of the scope of
248 * this method.
249 *
250 * Some camera hal may not support storing meta data in the video
251 * buffers, but all camera hal should support storing real YUV data
252 * in the video buffers. If the camera hal does not support storing
253 * the meta data in the video buffers when it is requested to do
254 * do, INVALID_OPERATION must be returned. It is very useful for
255 * the camera hal to pass meta data rather than the actual frame
256 * data directly to the video encoder, since the amount of the
257 * uncompressed frame data can be very large if video size is large.
258 *
259 * @param enable if true to instruct the camera hal to store
260 * meta data in the video buffers; false to instruct
261 * the camera hal to store real YUV data in the video
262 * buffers.
263 *
264 * @return OK on success.
265 */
266
267 status_t storeMetaDataInBuffers(int enable)
268 {
Steve Block3856b092011-10-20 11:56:00 +0100269 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700270 if (mDevice->ops->store_meta_data_in_buffers)
271 return mDevice->ops->store_meta_data_in_buffers(mDevice, enable);
272 return enable ? INVALID_OPERATION: OK;
273 }
274
275 /**
276 * Start record mode. When a record image is available a CAMERA_MSG_VIDEO_FRAME
277 * message is sent with the corresponding frame. Every record frame must be released
278 * by a cameral hal client via releaseRecordingFrame() before the client calls
279 * disableMsgType(CAMERA_MSG_VIDEO_FRAME). After the client calls
280 * disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's responsibility
281 * to manage the life-cycle of the video recording frames, and the client must
282 * not modify/access any video recording frames.
283 */
284 status_t startRecording()
285 {
Steve Block3856b092011-10-20 11:56:00 +0100286 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700287 if (mDevice->ops->start_recording)
288 return mDevice->ops->start_recording(mDevice);
289 return INVALID_OPERATION;
290 }
291
292 /**
293 * Stop a previously started recording.
294 */
295 void stopRecording()
296 {
Steve Block3856b092011-10-20 11:56:00 +0100297 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700298 if (mDevice->ops->stop_recording)
299 mDevice->ops->stop_recording(mDevice);
300 }
301
302 /**
303 * Returns true if recording is enabled.
304 */
305 int recordingEnabled()
306 {
Steve Block3856b092011-10-20 11:56:00 +0100307 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700308 if (mDevice->ops->recording_enabled)
309 return mDevice->ops->recording_enabled(mDevice);
310 return false;
311 }
312
313 /**
314 * Release a record frame previously returned by CAMERA_MSG_VIDEO_FRAME.
315 *
316 * It is camera hal client's responsibility to release video recording
317 * frames sent out by the camera hal before the camera hal receives
318 * a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME). After it receives
319 * the call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's
320 * responsibility of managing the life-cycle of the video recording
321 * frames.
322 */
323 void releaseRecordingFrame(const sp<IMemory>& mem)
324 {
Steve Block3856b092011-10-20 11:56:00 +0100325 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700326 if (mDevice->ops->release_recording_frame) {
327 ssize_t offset;
328 size_t size;
329 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
330 void *data = ((uint8_t *)heap->base()) + offset;
331 return mDevice->ops->release_recording_frame(mDevice, data);
332 }
333 }
334
335 /**
336 * Start auto focus, the notification callback routine is called
337 * with CAMERA_MSG_FOCUS once when focusing is complete. autoFocus()
338 * will be called again if another auto focus is needed.
339 */
340 status_t autoFocus()
341 {
Steve Block3856b092011-10-20 11:56:00 +0100342 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700343 if (mDevice->ops->auto_focus)
344 return mDevice->ops->auto_focus(mDevice);
345 return INVALID_OPERATION;
346 }
347
348 /**
349 * Cancels auto-focus function. If the auto-focus is still in progress,
350 * this function will cancel it. Whether the auto-focus is in progress
351 * or not, this function will return the focus position to the default.
352 * If the camera does not support auto-focus, this is a no-op.
353 */
354 status_t cancelAutoFocus()
355 {
Steve Block3856b092011-10-20 11:56:00 +0100356 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700357 if (mDevice->ops->cancel_auto_focus)
358 return mDevice->ops->cancel_auto_focus(mDevice);
359 return INVALID_OPERATION;
360 }
361
362 /**
363 * Take a picture.
364 */
365 status_t takePicture()
366 {
Steve Block3856b092011-10-20 11:56:00 +0100367 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700368 if (mDevice->ops->take_picture)
369 return mDevice->ops->take_picture(mDevice);
370 return INVALID_OPERATION;
371 }
372
373 /**
374 * Cancel a picture that was started with takePicture. Calling this
375 * method when no picture is being taken is a no-op.
376 */
377 status_t cancelPicture()
378 {
Steve Block3856b092011-10-20 11:56:00 +0100379 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700380 if (mDevice->ops->cancel_picture)
381 return mDevice->ops->cancel_picture(mDevice);
382 return INVALID_OPERATION;
383 }
384
385 /**
386 * Set the camera parameters. This returns BAD_VALUE if any parameter is
387 * invalid or not supported. */
388 status_t setParameters(const CameraParameters &params)
389 {
Steve Block3856b092011-10-20 11:56:00 +0100390 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700391 if (mDevice->ops->set_parameters)
392 return mDevice->ops->set_parameters(mDevice,
393 params.flatten().string());
394 return INVALID_OPERATION;
395 }
396
397 /** Return the camera parameters. */
398 CameraParameters getParameters() const
399 {
Steve Block3856b092011-10-20 11:56:00 +0100400 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700401 CameraParameters parms;
402 if (mDevice->ops->get_parameters) {
403 char *temp = mDevice->ops->get_parameters(mDevice);
404 String8 str_parms(temp);
Iliyan Malchev85fb61e2011-07-26 15:56:44 -0700405 if (mDevice->ops->put_parameters)
406 mDevice->ops->put_parameters(mDevice, temp);
407 else
408 free(temp);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700409 parms.unflatten(str_parms);
410 }
411 return parms;
412 }
413
414 /**
415 * Send command to camera driver.
416 */
417 status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
418 {
Steve Block3856b092011-10-20 11:56:00 +0100419 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700420 if (mDevice->ops->send_command)
421 return mDevice->ops->send_command(mDevice, cmd, arg1, arg2);
422 return INVALID_OPERATION;
423 }
424
425 /**
426 * Release the hardware resources owned by this object. Note that this is
427 * *not* done in the destructor.
428 */
429 void release() {
Steve Block3856b092011-10-20 11:56:00 +0100430 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700431 if (mDevice->ops->release)
432 mDevice->ops->release(mDevice);
433 }
434
435 /**
436 * Dump state of the camera hardware
437 */
Igor Murashkinddf3c502012-10-12 16:56:11 -0700438 status_t dump(int fd, const Vector<String16>& /*args*/) const
Iliyan Malchev8951a972011-04-14 16:55:59 -0700439 {
Steve Block3856b092011-10-20 11:56:00 +0100440 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700441 if (mDevice->ops->dump)
442 return mDevice->ops->dump(mDevice, fd);
443 return OK; // It's fine if the HAL doesn't implement dump()
444 }
445
446private:
447 camera_device_t *mDevice;
448 String8 mName;
449
450 static void __notify_cb(int32_t msg_type, int32_t ext1,
451 int32_t ext2, void *user)
452 {
Steve Block3856b092011-10-20 11:56:00 +0100453 ALOGV("%s", __FUNCTION__);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700454 CameraHardwareInterface *__this =
455 static_cast<CameraHardwareInterface *>(user);
456 __this->mNotifyCb(msg_type, ext1, ext2, __this->mCbUser);
457 }
458
459 static void __data_cb(int32_t msg_type,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700460 const camera_memory_t *data, unsigned int index,
Wu-cheng Liff09ef82011-07-28 05:30:59 +0800461 camera_frame_metadata_t *metadata,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700462 void *user)
463 {
Steve Block3856b092011-10-20 11:56:00 +0100464 ALOGV("%s", __FUNCTION__);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700465 CameraHardwareInterface *__this =
466 static_cast<CameraHardwareInterface *>(user);
467 sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(data->handle));
Iliyan Malchev26adde82011-06-06 17:21:32 -0700468 if (index >= mem->mNumBufs) {
Steve Block29357bc2012-01-06 19:20:56 +0000469 ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700470 index, mem->mNumBufs);
471 return;
472 }
Wu-cheng Liff09ef82011-07-28 05:30:59 +0800473 __this->mDataCb(msg_type, mem->mBuffers[index], metadata, __this->mCbUser);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700474 }
475
476 static void __data_cb_timestamp(nsecs_t timestamp, int32_t msg_type,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700477 const camera_memory_t *data, unsigned index,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700478 void *user)
479 {
Steve Block3856b092011-10-20 11:56:00 +0100480 ALOGV("%s", __FUNCTION__);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700481 CameraHardwareInterface *__this =
482 static_cast<CameraHardwareInterface *>(user);
483 // Start refcounting the heap object from here on. When the clients
484 // drop all references, it will be destroyed (as well as the enclosed
485 // MemoryHeapBase.
486 sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(data->handle));
Iliyan Malchev26adde82011-06-06 17:21:32 -0700487 if (index >= mem->mNumBufs) {
Steve Block29357bc2012-01-06 19:20:56 +0000488 ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700489 index, mem->mNumBufs);
490 return;
491 }
492 __this->mDataCbTimestamp(timestamp, msg_type, mem->mBuffers[index], __this->mCbUser);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700493 }
494
495 // This is a utility class that combines a MemoryHeapBase and a MemoryBase
496 // in one. Since we tend to use them in a one-to-one relationship, this is
497 // handy.
498
Iliyan Malchev26adde82011-06-06 17:21:32 -0700499 class CameraHeapMemory : public RefBase {
Iliyan Malchev8951a972011-04-14 16:55:59 -0700500 public:
Iliyan Malchev26adde82011-06-06 17:21:32 -0700501 CameraHeapMemory(int fd, size_t buf_size, uint_t num_buffers = 1) :
502 mBufSize(buf_size),
503 mNumBufs(num_buffers)
Iliyan Malchev8951a972011-04-14 16:55:59 -0700504 {
Iliyan Malchev26adde82011-06-06 17:21:32 -0700505 mHeap = new MemoryHeapBase(fd, buf_size * num_buffers);
506 commonInitialization();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700507 }
508
Iliyan Malchev26adde82011-06-06 17:21:32 -0700509 CameraHeapMemory(size_t buf_size, uint_t num_buffers = 1) :
510 mBufSize(buf_size),
511 mNumBufs(num_buffers)
512 {
513 mHeap = new MemoryHeapBase(buf_size * num_buffers);
514 commonInitialization();
515 }
516
517 void commonInitialization()
518 {
519 handle.data = mHeap->base();
520 handle.size = mBufSize * mNumBufs;
521 handle.handle = this;
522
523 mBuffers = new sp<MemoryBase>[mNumBufs];
524 for (uint_t i = 0; i < mNumBufs; i++)
525 mBuffers[i] = new MemoryBase(mHeap,
526 i * mBufSize,
527 mBufSize);
528
529 handle.release = __put_memory;
530 }
531
532 virtual ~CameraHeapMemory()
533 {
534 delete [] mBuffers;
535 }
536
537 size_t mBufSize;
538 uint_t mNumBufs;
539 sp<MemoryHeapBase> mHeap;
540 sp<MemoryBase> *mBuffers;
541
Iliyan Malchev8951a972011-04-14 16:55:59 -0700542 camera_memory_t handle;
543 };
544
Iliyan Malchev26adde82011-06-06 17:21:32 -0700545 static camera_memory_t* __get_memory(int fd, size_t buf_size, uint_t num_bufs,
546 void *user __attribute__((unused)))
Iliyan Malchev8951a972011-04-14 16:55:59 -0700547 {
Iliyan Malchev26adde82011-06-06 17:21:32 -0700548 CameraHeapMemory *mem;
549 if (fd < 0)
550 mem = new CameraHeapMemory(buf_size, num_bufs);
551 else
552 mem = new CameraHeapMemory(fd, buf_size, num_bufs);
553 mem->incStrong(mem);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700554 return &mem->handle;
555 }
556
Iliyan Malchev26adde82011-06-06 17:21:32 -0700557 static void __put_memory(camera_memory_t *data)
558 {
559 if (!data)
560 return;
561
562 CameraHeapMemory *mem = static_cast<CameraHeapMemory *>(data->handle);
563 mem->decStrong(mem);
564 }
565
Iliyan Malchev8951a972011-04-14 16:55:59 -0700566 static ANativeWindow *__to_anw(void *user)
567 {
568 CameraHardwareInterface *__this =
569 reinterpret_cast<CameraHardwareInterface *>(user);
570 return __this->mPreviewWindow.get();
571 }
572#define anw(n) __to_anw(((struct camera_preview_window *)n)->user)
573
574 static int __dequeue_buffer(struct preview_stream_ops* w,
Iliyan Malchevafcedc92011-06-10 16:05:23 -0700575 buffer_handle_t** buffer, int *stride)
Iliyan Malchev8951a972011-04-14 16:55:59 -0700576 {
577 int rc;
578 ANativeWindow *a = anw(w);
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700579 ANativeWindowBuffer* anb;
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700580 rc = native_window_dequeue_buffer_and_wait(a, &anb);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700581 if (!rc) {
Sundar Raman1e06f432011-06-17 09:05:09 -0500582 *buffer = &anb->handle;
583 *stride = anb->stride;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700584 }
585 return rc;
586 }
587
588#ifndef container_of
589#define container_of(ptr, type, member) ({ \
Dan Albert36802bd2014-11-20 11:31:17 -0800590 const __typeof__(((type *) 0)->member) *__mptr = (ptr); \
Iliyan Malchev8951a972011-04-14 16:55:59 -0700591 (type *) ((char *) __mptr - (char *)(&((type *)0)->member)); })
592#endif
593
Sundar Raman1e06f432011-06-17 09:05:09 -0500594 static int __lock_buffer(struct preview_stream_ops* w,
Igor Murashkinddf3c502012-10-12 16:56:11 -0700595 buffer_handle_t* /*buffer*/)
Sundar Raman1e06f432011-06-17 09:05:09 -0500596 {
597 ANativeWindow *a = anw(w);
Igor Murashkinddf3c502012-10-12 16:56:11 -0700598 (void)a;
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700599 return 0;
Sundar Raman1e06f432011-06-17 09:05:09 -0500600 }
601
Iliyan Malchev8951a972011-04-14 16:55:59 -0700602 static int __enqueue_buffer(struct preview_stream_ops* w,
603 buffer_handle_t* buffer)
604 {
605 ANativeWindow *a = anw(w);
606 return a->queueBuffer(a,
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700607 container_of(buffer, ANativeWindowBuffer, handle), -1);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700608 }
609
610 static int __cancel_buffer(struct preview_stream_ops* w,
611 buffer_handle_t* buffer)
612 {
613 ANativeWindow *a = anw(w);
614 return a->cancelBuffer(a,
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700615 container_of(buffer, ANativeWindowBuffer, handle), -1);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700616 }
617
618 static int __set_buffer_count(struct preview_stream_ops* w, int count)
619 {
620 ANativeWindow *a = anw(w);
Iliyan Malchev26adde82011-06-06 17:21:32 -0700621 return native_window_set_buffer_count(a, count);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700622 }
623
624 static int __set_buffers_geometry(struct preview_stream_ops* w,
625 int width, int height, int format)
626 {
Pierre Couillaudc09cec72014-07-03 10:55:00 -0700627 int rc;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700628 ANativeWindow *a = anw(w);
Pierre Couillaudc09cec72014-07-03 10:55:00 -0700629
630 rc = native_window_set_buffers_dimensions(a, width, height);
631 if (!rc) {
632 rc = native_window_set_buffers_format(a, format);
633 }
634 return rc;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700635 }
636
637 static int __set_crop(struct preview_stream_ops *w,
638 int left, int top, int right, int bottom)
639 {
640 ANativeWindow *a = anw(w);
641 android_native_rect_t crop;
642 crop.left = left;
643 crop.top = top;
644 crop.right = right;
645 crop.bottom = bottom;
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700646 return native_window_set_crop(a, &crop);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700647 }
648
Eino-Ville Talvala9f3d5912011-07-26 14:06:07 -0700649 static int __set_timestamp(struct preview_stream_ops *w,
650 int64_t timestamp) {
651 ANativeWindow *a = anw(w);
652 return native_window_set_buffers_timestamp(a, timestamp);
653 }
654
Iliyan Malchev8951a972011-04-14 16:55:59 -0700655 static int __set_usage(struct preview_stream_ops* w, int usage)
656 {
657 ANativeWindow *a = anw(w);
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700658 return native_window_set_usage(a, usage);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700659 }
660
661 static int __set_swap_interval(struct preview_stream_ops *w, int interval)
662 {
663 ANativeWindow *a = anw(w);
664 return a->setSwapInterval(a, interval);
665 }
666
667 static int __get_min_undequeued_buffer_count(
668 const struct preview_stream_ops *w,
669 int *count)
670 {
671 ANativeWindow *a = anw(w);
672 return a->query(a, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, count);
673 }
674
675 void initHalPreviewWindow()
676 {
677 mHalPreviewWindow.nw.cancel_buffer = __cancel_buffer;
Sundar Raman1e06f432011-06-17 09:05:09 -0500678 mHalPreviewWindow.nw.lock_buffer = __lock_buffer;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700679 mHalPreviewWindow.nw.dequeue_buffer = __dequeue_buffer;
680 mHalPreviewWindow.nw.enqueue_buffer = __enqueue_buffer;
681 mHalPreviewWindow.nw.set_buffer_count = __set_buffer_count;
682 mHalPreviewWindow.nw.set_buffers_geometry = __set_buffers_geometry;
683 mHalPreviewWindow.nw.set_crop = __set_crop;
Eino-Ville Talvala9f3d5912011-07-26 14:06:07 -0700684 mHalPreviewWindow.nw.set_timestamp = __set_timestamp;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700685 mHalPreviewWindow.nw.set_usage = __set_usage;
686 mHalPreviewWindow.nw.set_swap_interval = __set_swap_interval;
687
688 mHalPreviewWindow.nw.get_min_undequeued_buffer_count =
689 __get_min_undequeued_buffer_count;
690 }
691
692 sp<ANativeWindow> mPreviewWindow;
693
694 struct camera_preview_window {
695 struct preview_stream_ops nw;
696 void *user;
697 };
698
699 struct camera_preview_window mHalPreviewWindow;
700
701 notify_callback mNotifyCb;
702 data_callback mDataCb;
703 data_callback_timestamp mDataCbTimestamp;
704 void *mCbUser;
705};
706
707}; // namespace android
708
709#endif