blob: bce0762090642f9ee12597e4fcdf743d1bce9ea1 [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:
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -080076 CameraHardwareInterface(const char *name):
77 mDevice(nullptr),
78 mName(name),
79 mPreviewScalingMode(NOT_SET),
80 mPreviewTransform(NOT_SET),
81 mPreviewWidth(NOT_SET),
82 mPreviewHeight(NOT_SET),
83 mPreviewFormat(NOT_SET),
84 mPreviewUsage(0),
85 mPreviewSwapInterval(NOT_SET),
86 mPreviewCrop{NOT_SET,NOT_SET,NOT_SET,NOT_SET}
Iliyan Malchev8951a972011-04-14 16:55:59 -070087 {
Iliyan Malchev8951a972011-04-14 16:55:59 -070088 }
89
90 ~CameraHardwareInterface()
91 {
Steve Blockdf64d152012-01-04 20:05:49 +000092 ALOGI("Destroying camera %s", mName.string());
Tyler Luu5861a9a2011-10-06 00:00:03 -050093 if(mDevice) {
94 int rc = mDevice->common.close(&mDevice->common);
95 if (rc != OK)
Steve Block29357bc2012-01-06 19:20:56 +000096 ALOGE("Could not close camera %s: %d", mName.string(), rc);
Tyler Luu5861a9a2011-10-06 00:00:03 -050097 }
98 }
99
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800100 status_t initialize(CameraModule *module)
Tyler Luu5861a9a2011-10-06 00:00:03 -0500101 {
Steve Blockdf64d152012-01-04 20:05:49 +0000102 ALOGI("Opening camera %s", mName.string());
Zhijun Heb10cdad2014-06-16 16:38:35 -0700103 camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800104 status_t res = module->getCameraInfo(atoi(mName.string()), &info);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800105 if (res != OK) {
106 return res;
107 }
Zhijun Heb10cdad2014-06-16 16:38:35 -0700108
109 int rc = OK;
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800110 if (module->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_3 &&
Zhijun Heb10cdad2014-06-16 16:38:35 -0700111 info.device_version > CAMERA_DEVICE_API_VERSION_1_0) {
112 // Open higher version camera device as HAL1.0 device.
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800113 rc = module->openLegacy(mName.string(),
114 CAMERA_DEVICE_API_VERSION_1_0,
115 (hw_device_t **)&mDevice);
Zhijun Heb10cdad2014-06-16 16:38:35 -0700116 } else {
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800117 rc = module->open(mName.string(), (hw_device_t **)&mDevice);
Zhijun Heb10cdad2014-06-16 16:38:35 -0700118 }
Tyler Luu5861a9a2011-10-06 00:00:03 -0500119 if (rc != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000120 ALOGE("Could not open camera %s: %d", mName.string(), rc);
Tyler Luu5861a9a2011-10-06 00:00:03 -0500121 return rc;
122 }
123 initHalPreviewWindow();
124 return rc;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700125 }
126
127 /** Set the ANativeWindow to which preview frames are sent */
128 status_t setPreviewWindow(const sp<ANativeWindow>& buf)
129 {
Steve Block3856b092011-10-20 11:56:00 +0100130 ALOGV("%s(%s) buf %p", __FUNCTION__, mName.string(), buf.get());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700131 if (mDevice->ops->set_preview_window) {
132 mPreviewWindow = buf;
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800133 if (buf != nullptr) {
134 if (mPreviewScalingMode != NOT_SET) {
135 setPreviewScalingMode(mPreviewScalingMode);
136 }
137 if (mPreviewTransform != NOT_SET) {
138 setPreviewTransform(mPreviewTransform);
139 }
140 }
Iliyan Malchev8951a972011-04-14 16:55:59 -0700141 mHalPreviewWindow.user = this;
Steve Block3856b092011-10-20 11:56:00 +0100142 ALOGV("%s &mHalPreviewWindow %p mHalPreviewWindow.user %p", __FUNCTION__,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700143 &mHalPreviewWindow, mHalPreviewWindow.user);
144 return mDevice->ops->set_preview_window(mDevice,
145 buf.get() ? &mHalPreviewWindow.nw : 0);
146 }
147 return INVALID_OPERATION;
148 }
149
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800150 status_t setPreviewScalingMode(int scalingMode)
151 {
152 int rc = OK;
153 mPreviewScalingMode = scalingMode;
154 if (mPreviewWindow != nullptr) {
155 rc = native_window_set_scaling_mode(mPreviewWindow.get(),
156 scalingMode);
157 }
158 return rc;
159 }
160
161 status_t setPreviewTransform(int transform) {
162 int rc = OK;
163 mPreviewTransform = transform;
164 if (mPreviewWindow != nullptr) {
165 rc = native_window_set_buffers_transform(mPreviewWindow.get(),
166 mPreviewTransform);
167 }
168 return rc;
169 }
170
Iliyan Malchev8951a972011-04-14 16:55:59 -0700171 /** Set the notification and data callbacks */
172 void setCallbacks(notify_callback notify_cb,
173 data_callback data_cb,
174 data_callback_timestamp data_cb_timestamp,
175 void* user)
176 {
177 mNotifyCb = notify_cb;
178 mDataCb = data_cb;
179 mDataCbTimestamp = data_cb_timestamp;
180 mCbUser = user;
181
Steve Block3856b092011-10-20 11:56:00 +0100182 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700183
184 if (mDevice->ops->set_callbacks) {
185 mDevice->ops->set_callbacks(mDevice,
186 __notify_cb,
187 __data_cb,
188 __data_cb_timestamp,
189 __get_memory,
190 this);
191 }
192 }
193
194 /**
195 * The following three functions all take a msgtype,
196 * which is a bitmask of the messages defined in
197 * include/ui/Camera.h
198 */
199
200 /**
201 * Enable a message, or set of messages.
202 */
203 void enableMsgType(int32_t msgType)
204 {
Steve Block3856b092011-10-20 11:56:00 +0100205 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700206 if (mDevice->ops->enable_msg_type)
207 mDevice->ops->enable_msg_type(mDevice, msgType);
208 }
209
210 /**
211 * Disable a message, or a set of messages.
212 *
213 * Once received a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), camera hal
214 * should not rely on its client to call releaseRecordingFrame() to release
215 * video recording frames sent out by the cameral hal before and after the
216 * disableMsgType(CAMERA_MSG_VIDEO_FRAME) call. Camera hal clients must not
217 * modify/access any video recording frame after calling
218 * disableMsgType(CAMERA_MSG_VIDEO_FRAME).
219 */
220 void disableMsgType(int32_t msgType)
221 {
Steve Block3856b092011-10-20 11:56:00 +0100222 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700223 if (mDevice->ops->disable_msg_type)
224 mDevice->ops->disable_msg_type(mDevice, msgType);
225 }
226
227 /**
228 * Query whether a message, or a set of messages, is enabled.
229 * Note that this is operates as an AND, if any of the messages
230 * queried are off, this will return false.
231 */
232 int msgTypeEnabled(int32_t msgType)
233 {
Steve Block3856b092011-10-20 11:56:00 +0100234 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700235 if (mDevice->ops->msg_type_enabled)
236 return mDevice->ops->msg_type_enabled(mDevice, msgType);
237 return false;
238 }
239
240 /**
241 * Start preview mode.
242 */
243 status_t startPreview()
244 {
Steve Block3856b092011-10-20 11:56:00 +0100245 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700246 if (mDevice->ops->start_preview)
247 return mDevice->ops->start_preview(mDevice);
248 return INVALID_OPERATION;
249 }
250
251 /**
252 * Stop a previously started preview.
253 */
254 void stopPreview()
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->stop_preview)
258 mDevice->ops->stop_preview(mDevice);
259 }
260
261 /**
262 * Returns true if preview is enabled.
263 */
264 int previewEnabled()
265 {
Steve Block3856b092011-10-20 11:56:00 +0100266 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700267 if (mDevice->ops->preview_enabled)
268 return mDevice->ops->preview_enabled(mDevice);
269 return false;
270 }
271
272 /**
273 * Request the camera hal to store meta data or real YUV data in
274 * the video buffers send out via CAMERA_MSG_VIDEO_FRRAME for a
275 * recording session. If it is not called, the default camera
276 * hal behavior is to store real YUV data in the video buffers.
277 *
278 * This method should be called before startRecording() in order
279 * to be effective.
280 *
281 * If meta data is stored in the video buffers, it is up to the
282 * receiver of the video buffers to interpret the contents and
283 * to find the actual frame data with the help of the meta data
284 * in the buffer. How this is done is outside of the scope of
285 * this method.
286 *
287 * Some camera hal may not support storing meta data in the video
288 * buffers, but all camera hal should support storing real YUV data
289 * in the video buffers. If the camera hal does not support storing
290 * the meta data in the video buffers when it is requested to do
291 * do, INVALID_OPERATION must be returned. It is very useful for
292 * the camera hal to pass meta data rather than the actual frame
293 * data directly to the video encoder, since the amount of the
294 * uncompressed frame data can be very large if video size is large.
295 *
296 * @param enable if true to instruct the camera hal to store
297 * meta data in the video buffers; false to instruct
298 * the camera hal to store real YUV data in the video
299 * buffers.
300 *
301 * @return OK on success.
302 */
303
304 status_t storeMetaDataInBuffers(int enable)
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->store_meta_data_in_buffers)
308 return mDevice->ops->store_meta_data_in_buffers(mDevice, enable);
309 return enable ? INVALID_OPERATION: OK;
310 }
311
312 /**
313 * Start record mode. When a record image is available a CAMERA_MSG_VIDEO_FRAME
314 * message is sent with the corresponding frame. Every record frame must be released
315 * by a cameral hal client via releaseRecordingFrame() before the client calls
316 * disableMsgType(CAMERA_MSG_VIDEO_FRAME). After the client calls
317 * disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's responsibility
318 * to manage the life-cycle of the video recording frames, and the client must
319 * not modify/access any video recording frames.
320 */
321 status_t startRecording()
322 {
Steve Block3856b092011-10-20 11:56:00 +0100323 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700324 if (mDevice->ops->start_recording)
325 return mDevice->ops->start_recording(mDevice);
326 return INVALID_OPERATION;
327 }
328
329 /**
330 * Stop a previously started recording.
331 */
332 void stopRecording()
333 {
Steve Block3856b092011-10-20 11:56:00 +0100334 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700335 if (mDevice->ops->stop_recording)
336 mDevice->ops->stop_recording(mDevice);
337 }
338
339 /**
340 * Returns true if recording is enabled.
341 */
342 int recordingEnabled()
343 {
Steve Block3856b092011-10-20 11:56:00 +0100344 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700345 if (mDevice->ops->recording_enabled)
346 return mDevice->ops->recording_enabled(mDevice);
347 return false;
348 }
349
350 /**
351 * Release a record frame previously returned by CAMERA_MSG_VIDEO_FRAME.
352 *
353 * It is camera hal client's responsibility to release video recording
354 * frames sent out by the camera hal before the camera hal receives
355 * a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME). After it receives
356 * the call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's
357 * responsibility of managing the life-cycle of the video recording
358 * frames.
359 */
360 void releaseRecordingFrame(const sp<IMemory>& mem)
361 {
Steve Block3856b092011-10-20 11:56:00 +0100362 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700363 if (mDevice->ops->release_recording_frame) {
364 ssize_t offset;
365 size_t size;
366 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
367 void *data = ((uint8_t *)heap->base()) + offset;
368 return mDevice->ops->release_recording_frame(mDevice, data);
369 }
370 }
371
372 /**
373 * Start auto focus, the notification callback routine is called
374 * with CAMERA_MSG_FOCUS once when focusing is complete. autoFocus()
375 * will be called again if another auto focus is needed.
376 */
377 status_t autoFocus()
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->auto_focus)
381 return mDevice->ops->auto_focus(mDevice);
382 return INVALID_OPERATION;
383 }
384
385 /**
386 * Cancels auto-focus function. If the auto-focus is still in progress,
387 * this function will cancel it. Whether the auto-focus is in progress
388 * or not, this function will return the focus position to the default.
389 * If the camera does not support auto-focus, this is a no-op.
390 */
391 status_t cancelAutoFocus()
392 {
Steve Block3856b092011-10-20 11:56:00 +0100393 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700394 if (mDevice->ops->cancel_auto_focus)
395 return mDevice->ops->cancel_auto_focus(mDevice);
396 return INVALID_OPERATION;
397 }
398
399 /**
400 * Take a picture.
401 */
402 status_t takePicture()
403 {
Steve Block3856b092011-10-20 11:56:00 +0100404 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700405 if (mDevice->ops->take_picture)
406 return mDevice->ops->take_picture(mDevice);
407 return INVALID_OPERATION;
408 }
409
410 /**
411 * Cancel a picture that was started with takePicture. Calling this
412 * method when no picture is being taken is a no-op.
413 */
414 status_t cancelPicture()
415 {
Steve Block3856b092011-10-20 11:56:00 +0100416 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700417 if (mDevice->ops->cancel_picture)
418 return mDevice->ops->cancel_picture(mDevice);
419 return INVALID_OPERATION;
420 }
421
422 /**
423 * Set the camera parameters. This returns BAD_VALUE if any parameter is
424 * invalid or not supported. */
425 status_t setParameters(const CameraParameters &params)
426 {
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->set_parameters)
429 return mDevice->ops->set_parameters(mDevice,
430 params.flatten().string());
431 return INVALID_OPERATION;
432 }
433
434 /** Return the camera parameters. */
435 CameraParameters getParameters() const
436 {
Steve Block3856b092011-10-20 11:56:00 +0100437 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700438 CameraParameters parms;
439 if (mDevice->ops->get_parameters) {
440 char *temp = mDevice->ops->get_parameters(mDevice);
441 String8 str_parms(temp);
Iliyan Malchev85fb61e2011-07-26 15:56:44 -0700442 if (mDevice->ops->put_parameters)
443 mDevice->ops->put_parameters(mDevice, temp);
444 else
445 free(temp);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700446 parms.unflatten(str_parms);
447 }
448 return parms;
449 }
450
451 /**
452 * Send command to camera driver.
453 */
454 status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
455 {
Steve Block3856b092011-10-20 11:56:00 +0100456 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700457 if (mDevice->ops->send_command)
458 return mDevice->ops->send_command(mDevice, cmd, arg1, arg2);
459 return INVALID_OPERATION;
460 }
461
462 /**
463 * Release the hardware resources owned by this object. Note that this is
464 * *not* done in the destructor.
465 */
466 void release() {
Steve Block3856b092011-10-20 11:56:00 +0100467 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700468 if (mDevice->ops->release)
469 mDevice->ops->release(mDevice);
470 }
471
472 /**
473 * Dump state of the camera hardware
474 */
Igor Murashkinddf3c502012-10-12 16:56:11 -0700475 status_t dump(int fd, const Vector<String16>& /*args*/) const
Iliyan Malchev8951a972011-04-14 16:55:59 -0700476 {
Steve Block3856b092011-10-20 11:56:00 +0100477 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700478 if (mDevice->ops->dump)
479 return mDevice->ops->dump(mDevice, fd);
480 return OK; // It's fine if the HAL doesn't implement dump()
481 }
482
483private:
484 camera_device_t *mDevice;
485 String8 mName;
486
487 static void __notify_cb(int32_t msg_type, int32_t ext1,
488 int32_t ext2, void *user)
489 {
Steve Block3856b092011-10-20 11:56:00 +0100490 ALOGV("%s", __FUNCTION__);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700491 CameraHardwareInterface *__this =
492 static_cast<CameraHardwareInterface *>(user);
493 __this->mNotifyCb(msg_type, ext1, ext2, __this->mCbUser);
494 }
495
496 static void __data_cb(int32_t msg_type,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700497 const camera_memory_t *data, unsigned int index,
Wu-cheng Liff09ef82011-07-28 05:30:59 +0800498 camera_frame_metadata_t *metadata,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700499 void *user)
500 {
Steve Block3856b092011-10-20 11:56:00 +0100501 ALOGV("%s", __FUNCTION__);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700502 CameraHardwareInterface *__this =
503 static_cast<CameraHardwareInterface *>(user);
504 sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(data->handle));
Iliyan Malchev26adde82011-06-06 17:21:32 -0700505 if (index >= mem->mNumBufs) {
Steve Block29357bc2012-01-06 19:20:56 +0000506 ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700507 index, mem->mNumBufs);
508 return;
509 }
Wu-cheng Liff09ef82011-07-28 05:30:59 +0800510 __this->mDataCb(msg_type, mem->mBuffers[index], metadata, __this->mCbUser);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700511 }
512
513 static void __data_cb_timestamp(nsecs_t timestamp, int32_t msg_type,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700514 const camera_memory_t *data, unsigned index,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700515 void *user)
516 {
Steve Block3856b092011-10-20 11:56:00 +0100517 ALOGV("%s", __FUNCTION__);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700518 CameraHardwareInterface *__this =
519 static_cast<CameraHardwareInterface *>(user);
520 // Start refcounting the heap object from here on. When the clients
521 // drop all references, it will be destroyed (as well as the enclosed
522 // MemoryHeapBase.
523 sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(data->handle));
Iliyan Malchev26adde82011-06-06 17:21:32 -0700524 if (index >= mem->mNumBufs) {
Steve Block29357bc2012-01-06 19:20:56 +0000525 ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700526 index, mem->mNumBufs);
527 return;
528 }
529 __this->mDataCbTimestamp(timestamp, msg_type, mem->mBuffers[index], __this->mCbUser);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700530 }
531
532 // This is a utility class that combines a MemoryHeapBase and a MemoryBase
533 // in one. Since we tend to use them in a one-to-one relationship, this is
534 // handy.
535
Iliyan Malchev26adde82011-06-06 17:21:32 -0700536 class CameraHeapMemory : public RefBase {
Iliyan Malchev8951a972011-04-14 16:55:59 -0700537 public:
Iliyan Malchev26adde82011-06-06 17:21:32 -0700538 CameraHeapMemory(int fd, size_t buf_size, uint_t num_buffers = 1) :
539 mBufSize(buf_size),
540 mNumBufs(num_buffers)
Iliyan Malchev8951a972011-04-14 16:55:59 -0700541 {
Iliyan Malchev26adde82011-06-06 17:21:32 -0700542 mHeap = new MemoryHeapBase(fd, buf_size * num_buffers);
543 commonInitialization();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700544 }
545
Iliyan Malchev26adde82011-06-06 17:21:32 -0700546 CameraHeapMemory(size_t buf_size, uint_t num_buffers = 1) :
547 mBufSize(buf_size),
548 mNumBufs(num_buffers)
549 {
550 mHeap = new MemoryHeapBase(buf_size * num_buffers);
551 commonInitialization();
552 }
553
554 void commonInitialization()
555 {
556 handle.data = mHeap->base();
557 handle.size = mBufSize * mNumBufs;
558 handle.handle = this;
559
560 mBuffers = new sp<MemoryBase>[mNumBufs];
561 for (uint_t i = 0; i < mNumBufs; i++)
562 mBuffers[i] = new MemoryBase(mHeap,
563 i * mBufSize,
564 mBufSize);
565
566 handle.release = __put_memory;
567 }
568
569 virtual ~CameraHeapMemory()
570 {
571 delete [] mBuffers;
572 }
573
574 size_t mBufSize;
575 uint_t mNumBufs;
576 sp<MemoryHeapBase> mHeap;
577 sp<MemoryBase> *mBuffers;
578
Iliyan Malchev8951a972011-04-14 16:55:59 -0700579 camera_memory_t handle;
580 };
581
Iliyan Malchev26adde82011-06-06 17:21:32 -0700582 static camera_memory_t* __get_memory(int fd, size_t buf_size, uint_t num_bufs,
583 void *user __attribute__((unused)))
Iliyan Malchev8951a972011-04-14 16:55:59 -0700584 {
Iliyan Malchev26adde82011-06-06 17:21:32 -0700585 CameraHeapMemory *mem;
586 if (fd < 0)
587 mem = new CameraHeapMemory(buf_size, num_bufs);
588 else
589 mem = new CameraHeapMemory(fd, buf_size, num_bufs);
590 mem->incStrong(mem);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700591 return &mem->handle;
592 }
593
Iliyan Malchev26adde82011-06-06 17:21:32 -0700594 static void __put_memory(camera_memory_t *data)
595 {
596 if (!data)
597 return;
598
599 CameraHeapMemory *mem = static_cast<CameraHeapMemory *>(data->handle);
600 mem->decStrong(mem);
601 }
602
Iliyan Malchev8951a972011-04-14 16:55:59 -0700603 static ANativeWindow *__to_anw(void *user)
604 {
605 CameraHardwareInterface *__this =
606 reinterpret_cast<CameraHardwareInterface *>(user);
607 return __this->mPreviewWindow.get();
608 }
609#define anw(n) __to_anw(((struct camera_preview_window *)n)->user)
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800610#define hwi(n) reinterpret_cast<CameraHardwareInterface *>(\
611 ((struct camera_preview_window *)n)->user)
Iliyan Malchev8951a972011-04-14 16:55:59 -0700612
613 static int __dequeue_buffer(struct preview_stream_ops* w,
Iliyan Malchevafcedc92011-06-10 16:05:23 -0700614 buffer_handle_t** buffer, int *stride)
Iliyan Malchev8951a972011-04-14 16:55:59 -0700615 {
616 int rc;
617 ANativeWindow *a = anw(w);
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700618 ANativeWindowBuffer* anb;
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700619 rc = native_window_dequeue_buffer_and_wait(a, &anb);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700620 if (!rc) {
Sundar Raman1e06f432011-06-17 09:05:09 -0500621 *buffer = &anb->handle;
622 *stride = anb->stride;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700623 }
624 return rc;
625 }
626
627#ifndef container_of
628#define container_of(ptr, type, member) ({ \
Dan Albert36802bd2014-11-20 11:31:17 -0800629 const __typeof__(((type *) 0)->member) *__mptr = (ptr); \
Iliyan Malchev8951a972011-04-14 16:55:59 -0700630 (type *) ((char *) __mptr - (char *)(&((type *)0)->member)); })
631#endif
632
Sundar Raman1e06f432011-06-17 09:05:09 -0500633 static int __lock_buffer(struct preview_stream_ops* w,
Igor Murashkinddf3c502012-10-12 16:56:11 -0700634 buffer_handle_t* /*buffer*/)
Sundar Raman1e06f432011-06-17 09:05:09 -0500635 {
636 ANativeWindow *a = anw(w);
Igor Murashkinddf3c502012-10-12 16:56:11 -0700637 (void)a;
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700638 return 0;
Sundar Raman1e06f432011-06-17 09:05:09 -0500639 }
640
Iliyan Malchev8951a972011-04-14 16:55:59 -0700641 static int __enqueue_buffer(struct preview_stream_ops* w,
642 buffer_handle_t* buffer)
643 {
644 ANativeWindow *a = anw(w);
645 return a->queueBuffer(a,
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700646 container_of(buffer, ANativeWindowBuffer, handle), -1);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700647 }
648
649 static int __cancel_buffer(struct preview_stream_ops* w,
650 buffer_handle_t* buffer)
651 {
652 ANativeWindow *a = anw(w);
653 return a->cancelBuffer(a,
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700654 container_of(buffer, ANativeWindowBuffer, handle), -1);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700655 }
656
657 static int __set_buffer_count(struct preview_stream_ops* w, int count)
658 {
659 ANativeWindow *a = anw(w);
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800660
661 if (a != nullptr) {
662 // Workaround for b/27039775
663 // Previously, setting the buffer count would reset the buffer
664 // queue's flag that allows for all buffers to be dequeued on the
665 // producer side, instead of just the producer's declared max count,
666 // if no filled buffers have yet been queued by the producer. This
667 // reset no longer happens, but some HALs depend on this behavior,
668 // so it needs to be maintained for HAL backwards compatibility.
669 // Simulate the prior behavior by disconnecting/reconnecting to the
670 // window and setting the values again. This has the drawback of
671 // actually causing memory reallocation, which may not have happened
672 // in the past.
673 CameraHardwareInterface *hw = hwi(w);
674 native_window_api_disconnect(a, NATIVE_WINDOW_API_CAMERA);
675 native_window_api_connect(a, NATIVE_WINDOW_API_CAMERA);
676 if (hw->mPreviewScalingMode != NOT_SET) {
677 native_window_set_scaling_mode(a, hw->mPreviewScalingMode);
678 }
679 if (hw->mPreviewTransform != NOT_SET) {
680 native_window_set_buffers_transform(a, hw->mPreviewTransform);
681 }
682 if (hw->mPreviewWidth != NOT_SET) {
683 native_window_set_buffers_dimensions(a,
684 hw->mPreviewWidth, hw->mPreviewHeight);
685 native_window_set_buffers_format(a, hw->mPreviewFormat);
686 }
687 if (hw->mPreviewUsage != 0) {
688 native_window_set_usage(a, hw->mPreviewUsage);
689 }
690 if (hw->mPreviewSwapInterval != NOT_SET) {
691 a->setSwapInterval(a, hw->mPreviewSwapInterval);
692 }
693 if (hw->mPreviewCrop.left != NOT_SET) {
694 native_window_set_crop(a, &(hw->mPreviewCrop));
695 }
696 }
697
Iliyan Malchev26adde82011-06-06 17:21:32 -0700698 return native_window_set_buffer_count(a, count);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700699 }
700
701 static int __set_buffers_geometry(struct preview_stream_ops* w,
702 int width, int height, int format)
703 {
Pierre Couillaudc09cec72014-07-03 10:55:00 -0700704 int rc;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700705 ANativeWindow *a = anw(w);
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800706 CameraHardwareInterface *hw = hwi(w);
707 hw->mPreviewWidth = width;
708 hw->mPreviewHeight = height;
709 hw->mPreviewFormat = format;
Pierre Couillaudc09cec72014-07-03 10:55:00 -0700710 rc = native_window_set_buffers_dimensions(a, width, height);
711 if (!rc) {
712 rc = native_window_set_buffers_format(a, format);
713 }
714 return rc;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700715 }
716
717 static int __set_crop(struct preview_stream_ops *w,
718 int left, int top, int right, int bottom)
719 {
720 ANativeWindow *a = anw(w);
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800721 CameraHardwareInterface *hw = hwi(w);
722 hw->mPreviewCrop.left = left;
723 hw->mPreviewCrop.top = top;
724 hw->mPreviewCrop.right = right;
725 hw->mPreviewCrop.bottom = bottom;
726 return native_window_set_crop(a, &(hw->mPreviewCrop));
Iliyan Malchev8951a972011-04-14 16:55:59 -0700727 }
728
Eino-Ville Talvala9f3d5912011-07-26 14:06:07 -0700729 static int __set_timestamp(struct preview_stream_ops *w,
730 int64_t timestamp) {
731 ANativeWindow *a = anw(w);
732 return native_window_set_buffers_timestamp(a, timestamp);
733 }
734
Iliyan Malchev8951a972011-04-14 16:55:59 -0700735 static int __set_usage(struct preview_stream_ops* w, int usage)
736 {
737 ANativeWindow *a = anw(w);
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800738 CameraHardwareInterface *hw = hwi(w);
739 hw->mPreviewUsage = usage;
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700740 return native_window_set_usage(a, usage);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700741 }
742
743 static int __set_swap_interval(struct preview_stream_ops *w, int interval)
744 {
745 ANativeWindow *a = anw(w);
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800746 CameraHardwareInterface *hw = hwi(w);
747 hw->mPreviewSwapInterval = interval;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700748 return a->setSwapInterval(a, interval);
749 }
750
751 static int __get_min_undequeued_buffer_count(
752 const struct preview_stream_ops *w,
753 int *count)
754 {
755 ANativeWindow *a = anw(w);
756 return a->query(a, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, count);
757 }
758
759 void initHalPreviewWindow()
760 {
761 mHalPreviewWindow.nw.cancel_buffer = __cancel_buffer;
Sundar Raman1e06f432011-06-17 09:05:09 -0500762 mHalPreviewWindow.nw.lock_buffer = __lock_buffer;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700763 mHalPreviewWindow.nw.dequeue_buffer = __dequeue_buffer;
764 mHalPreviewWindow.nw.enqueue_buffer = __enqueue_buffer;
765 mHalPreviewWindow.nw.set_buffer_count = __set_buffer_count;
766 mHalPreviewWindow.nw.set_buffers_geometry = __set_buffers_geometry;
767 mHalPreviewWindow.nw.set_crop = __set_crop;
Eino-Ville Talvala9f3d5912011-07-26 14:06:07 -0700768 mHalPreviewWindow.nw.set_timestamp = __set_timestamp;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700769 mHalPreviewWindow.nw.set_usage = __set_usage;
770 mHalPreviewWindow.nw.set_swap_interval = __set_swap_interval;
771
772 mHalPreviewWindow.nw.get_min_undequeued_buffer_count =
773 __get_min_undequeued_buffer_count;
774 }
775
776 sp<ANativeWindow> mPreviewWindow;
777
778 struct camera_preview_window {
779 struct preview_stream_ops nw;
780 void *user;
781 };
782
783 struct camera_preview_window mHalPreviewWindow;
784
785 notify_callback mNotifyCb;
786 data_callback mDataCb;
787 data_callback_timestamp mDataCbTimestamp;
788 void *mCbUser;
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800789
790 // Cached values for preview stream parameters
791 static const int NOT_SET = -1;
792 int mPreviewScalingMode;
793 int mPreviewTransform;
794 int mPreviewWidth;
795 int mPreviewHeight;
796 int mPreviewFormat;
797 int mPreviewUsage;
798 int mPreviewSwapInterval;
799 android_native_rect_t mPreviewCrop;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700800};
801
802}; // namespace android
803
804#endif