blob: c8210b7e121950e4b6ada2781d4b277fb0e2b52c [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
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080030#include <common/CameraModule.h>
31#include <common/CameraProviderManager.h>
32
Iliyan Malchev8951a972011-04-14 16:55:59 -070033namespace android {
34
35typedef void (*notify_callback)(int32_t msgType,
36 int32_t ext1,
37 int32_t ext2,
38 void* user);
39
40typedef void (*data_callback)(int32_t msgType,
41 const sp<IMemory> &dataPtr,
Wu-cheng Liff09ef82011-07-28 05:30:59 +080042 camera_frame_metadata_t *metadata,
Iliyan Malchev8951a972011-04-14 16:55:59 -070043 void* user);
44
45typedef void (*data_callback_timestamp)(nsecs_t timestamp,
46 int32_t msgType,
47 const sp<IMemory> &dataPtr,
48 void *user);
49
50/**
51 * CameraHardwareInterface.h defines the interface to the
52 * camera hardware abstraction layer, used for setting and getting
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080053 * parameters, live previewing, and taking pictures. It is used for
54 * HAL devices with version CAMERA_DEVICE_API_VERSION_1_0 only.
Iliyan Malchev8951a972011-04-14 16:55:59 -070055 *
56 * It is a referenced counted interface with RefBase as its base class.
57 * CameraService calls openCameraHardware() to retrieve a strong pointer to the
58 * instance of this interface and may be called multiple times. The
59 * following steps describe a typical sequence:
60 *
61 * -# After CameraService calls openCameraHardware(), getParameters() and
62 * setParameters() are used to initialize the camera instance.
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080063 * -# startPreview() is called.
Iliyan Malchev8951a972011-04-14 16:55:59 -070064 *
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080065 * Prior to taking a picture, CameraService often calls autofocus(). When auto
Iliyan Malchev8951a972011-04-14 16:55:59 -070066 * focusing has completed, the camera instance sends a CAMERA_MSG_FOCUS notification,
67 * which informs the application whether focusing was successful. The camera instance
68 * only sends this message once and it is up to the application to call autoFocus()
69 * again if refocusing is desired.
70 *
71 * CameraService calls takePicture() to request the camera instance take a
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -080072 * picture. At this point, if a shutter, postview, raw, and/or compressed
73 * callback is desired, the corresponding message must be enabled. Any memory
74 * provided in a data callback must be copied if it's needed after returning.
Iliyan Malchev8951a972011-04-14 16:55:59 -070075 */
76
77class CameraHardwareInterface : public virtual RefBase {
78public:
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -070079 explicit CameraHardwareInterface(const char *name):
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -080080 mDevice(nullptr),
81 mName(name),
82 mPreviewScalingMode(NOT_SET),
83 mPreviewTransform(NOT_SET),
84 mPreviewWidth(NOT_SET),
85 mPreviewHeight(NOT_SET),
86 mPreviewFormat(NOT_SET),
87 mPreviewUsage(0),
88 mPreviewSwapInterval(NOT_SET),
89 mPreviewCrop{NOT_SET,NOT_SET,NOT_SET,NOT_SET}
Iliyan Malchev8951a972011-04-14 16:55:59 -070090 {
Iliyan Malchev8951a972011-04-14 16:55:59 -070091 }
92
93 ~CameraHardwareInterface()
94 {
Steve Blockdf64d152012-01-04 20:05:49 +000095 ALOGI("Destroying camera %s", mName.string());
Tyler Luu5861a9a2011-10-06 00:00:03 -050096 if(mDevice) {
97 int rc = mDevice->common.close(&mDevice->common);
98 if (rc != OK)
Steve Block29357bc2012-01-06 19:20:56 +000099 ALOGE("Could not close camera %s: %d", mName.string(), rc);
Tyler Luu5861a9a2011-10-06 00:00:03 -0500100 }
101 }
102
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800103 status_t initialize(CameraModule *module)
Tyler Luu5861a9a2011-10-06 00:00:03 -0500104 {
Steve Blockdf64d152012-01-04 20:05:49 +0000105 ALOGI("Opening camera %s", mName.string());
Zhijun Heb10cdad2014-06-16 16:38:35 -0700106 camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800107 status_t res = module->getCameraInfo(atoi(mName.string()), &info);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800108 if (res != OK) {
109 return res;
110 }
Zhijun Heb10cdad2014-06-16 16:38:35 -0700111
112 int rc = OK;
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800113 if (module->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_3 &&
Zhijun Heb10cdad2014-06-16 16:38:35 -0700114 info.device_version > CAMERA_DEVICE_API_VERSION_1_0) {
115 // Open higher version camera device as HAL1.0 device.
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800116 rc = module->openLegacy(mName.string(),
117 CAMERA_DEVICE_API_VERSION_1_0,
118 (hw_device_t **)&mDevice);
Zhijun Heb10cdad2014-06-16 16:38:35 -0700119 } else {
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800120 rc = module->open(mName.string(), (hw_device_t **)&mDevice);
Zhijun Heb10cdad2014-06-16 16:38:35 -0700121 }
Tyler Luu5861a9a2011-10-06 00:00:03 -0500122 if (rc != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000123 ALOGE("Could not open camera %s: %d", mName.string(), rc);
Tyler Luu5861a9a2011-10-06 00:00:03 -0500124 return rc;
125 }
126 initHalPreviewWindow();
127 return rc;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700128 }
129
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800130 status_t initialize(sp<CameraProviderManager> manager) {
131 (void) manager;
132 ALOGE("%s: Not supported yet", __FUNCTION__);
133 return INVALID_OPERATION;
134 }
135
Iliyan Malchev8951a972011-04-14 16:55:59 -0700136 /** Set the ANativeWindow to which preview frames are sent */
137 status_t setPreviewWindow(const sp<ANativeWindow>& buf)
138 {
Steve Block3856b092011-10-20 11:56:00 +0100139 ALOGV("%s(%s) buf %p", __FUNCTION__, mName.string(), buf.get());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700140 if (mDevice->ops->set_preview_window) {
141 mPreviewWindow = buf;
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800142 if (buf != nullptr) {
143 if (mPreviewScalingMode != NOT_SET) {
144 setPreviewScalingMode(mPreviewScalingMode);
145 }
146 if (mPreviewTransform != NOT_SET) {
147 setPreviewTransform(mPreviewTransform);
148 }
149 }
Iliyan Malchev8951a972011-04-14 16:55:59 -0700150 mHalPreviewWindow.user = this;
Steve Block3856b092011-10-20 11:56:00 +0100151 ALOGV("%s &mHalPreviewWindow %p mHalPreviewWindow.user %p", __FUNCTION__,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700152 &mHalPreviewWindow, mHalPreviewWindow.user);
153 return mDevice->ops->set_preview_window(mDevice,
154 buf.get() ? &mHalPreviewWindow.nw : 0);
155 }
156 return INVALID_OPERATION;
157 }
158
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800159 status_t setPreviewScalingMode(int scalingMode)
160 {
161 int rc = OK;
162 mPreviewScalingMode = scalingMode;
163 if (mPreviewWindow != nullptr) {
164 rc = native_window_set_scaling_mode(mPreviewWindow.get(),
165 scalingMode);
166 }
167 return rc;
168 }
169
170 status_t setPreviewTransform(int transform) {
171 int rc = OK;
172 mPreviewTransform = transform;
173 if (mPreviewWindow != nullptr) {
174 rc = native_window_set_buffers_transform(mPreviewWindow.get(),
175 mPreviewTransform);
176 }
177 return rc;
178 }
179
Iliyan Malchev8951a972011-04-14 16:55:59 -0700180 /** Set the notification and data callbacks */
181 void setCallbacks(notify_callback notify_cb,
182 data_callback data_cb,
183 data_callback_timestamp data_cb_timestamp,
184 void* user)
185 {
186 mNotifyCb = notify_cb;
187 mDataCb = data_cb;
188 mDataCbTimestamp = data_cb_timestamp;
189 mCbUser = user;
190
Steve Block3856b092011-10-20 11:56:00 +0100191 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700192
193 if (mDevice->ops->set_callbacks) {
194 mDevice->ops->set_callbacks(mDevice,
195 __notify_cb,
196 __data_cb,
197 __data_cb_timestamp,
198 __get_memory,
199 this);
200 }
201 }
202
203 /**
204 * The following three functions all take a msgtype,
205 * which is a bitmask of the messages defined in
206 * include/ui/Camera.h
207 */
208
209 /**
210 * Enable a message, or set of messages.
211 */
212 void enableMsgType(int32_t msgType)
213 {
Steve Block3856b092011-10-20 11:56:00 +0100214 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700215 if (mDevice->ops->enable_msg_type)
216 mDevice->ops->enable_msg_type(mDevice, msgType);
217 }
218
219 /**
220 * Disable a message, or a set of messages.
221 *
222 * Once received a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), camera hal
223 * should not rely on its client to call releaseRecordingFrame() to release
224 * video recording frames sent out by the cameral hal before and after the
225 * disableMsgType(CAMERA_MSG_VIDEO_FRAME) call. Camera hal clients must not
226 * modify/access any video recording frame after calling
227 * disableMsgType(CAMERA_MSG_VIDEO_FRAME).
228 */
229 void disableMsgType(int32_t msgType)
230 {
Steve Block3856b092011-10-20 11:56:00 +0100231 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700232 if (mDevice->ops->disable_msg_type)
233 mDevice->ops->disable_msg_type(mDevice, msgType);
234 }
235
236 /**
237 * Query whether a message, or a set of messages, is enabled.
238 * Note that this is operates as an AND, if any of the messages
239 * queried are off, this will return false.
240 */
241 int msgTypeEnabled(int32_t msgType)
242 {
Steve Block3856b092011-10-20 11:56:00 +0100243 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700244 if (mDevice->ops->msg_type_enabled)
245 return mDevice->ops->msg_type_enabled(mDevice, msgType);
246 return false;
247 }
248
249 /**
250 * Start preview mode.
251 */
252 status_t startPreview()
253 {
Steve Block3856b092011-10-20 11:56:00 +0100254 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700255 if (mDevice->ops->start_preview)
256 return mDevice->ops->start_preview(mDevice);
257 return INVALID_OPERATION;
258 }
259
260 /**
261 * Stop a previously started preview.
262 */
263 void stopPreview()
264 {
Steve Block3856b092011-10-20 11:56:00 +0100265 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700266 if (mDevice->ops->stop_preview)
267 mDevice->ops->stop_preview(mDevice);
268 }
269
270 /**
271 * Returns true if preview is enabled.
272 */
273 int previewEnabled()
274 {
Steve Block3856b092011-10-20 11:56:00 +0100275 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700276 if (mDevice->ops->preview_enabled)
277 return mDevice->ops->preview_enabled(mDevice);
278 return false;
279 }
280
281 /**
282 * Request the camera hal to store meta data or real YUV data in
283 * the video buffers send out via CAMERA_MSG_VIDEO_FRRAME for a
284 * recording session. If it is not called, the default camera
285 * hal behavior is to store real YUV data in the video buffers.
286 *
287 * This method should be called before startRecording() in order
288 * to be effective.
289 *
290 * If meta data is stored in the video buffers, it is up to the
291 * receiver of the video buffers to interpret the contents and
292 * to find the actual frame data with the help of the meta data
293 * in the buffer. How this is done is outside of the scope of
294 * this method.
295 *
296 * Some camera hal may not support storing meta data in the video
297 * buffers, but all camera hal should support storing real YUV data
298 * in the video buffers. If the camera hal does not support storing
299 * the meta data in the video buffers when it is requested to do
300 * do, INVALID_OPERATION must be returned. It is very useful for
301 * the camera hal to pass meta data rather than the actual frame
302 * data directly to the video encoder, since the amount of the
303 * uncompressed frame data can be very large if video size is large.
304 *
305 * @param enable if true to instruct the camera hal to store
306 * meta data in the video buffers; false to instruct
307 * the camera hal to store real YUV data in the video
308 * buffers.
309 *
310 * @return OK on success.
311 */
312
313 status_t storeMetaDataInBuffers(int enable)
314 {
Steve Block3856b092011-10-20 11:56:00 +0100315 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700316 if (mDevice->ops->store_meta_data_in_buffers)
317 return mDevice->ops->store_meta_data_in_buffers(mDevice, enable);
318 return enable ? INVALID_OPERATION: OK;
319 }
320
321 /**
322 * Start record mode. When a record image is available a CAMERA_MSG_VIDEO_FRAME
323 * message is sent with the corresponding frame. Every record frame must be released
324 * by a cameral hal client via releaseRecordingFrame() before the client calls
325 * disableMsgType(CAMERA_MSG_VIDEO_FRAME). After the client calls
326 * disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's responsibility
327 * to manage the life-cycle of the video recording frames, and the client must
328 * not modify/access any video recording frames.
329 */
330 status_t startRecording()
331 {
Steve Block3856b092011-10-20 11:56:00 +0100332 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700333 if (mDevice->ops->start_recording)
334 return mDevice->ops->start_recording(mDevice);
335 return INVALID_OPERATION;
336 }
337
338 /**
339 * Stop a previously started recording.
340 */
341 void stopRecording()
342 {
Steve Block3856b092011-10-20 11:56:00 +0100343 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700344 if (mDevice->ops->stop_recording)
345 mDevice->ops->stop_recording(mDevice);
346 }
347
348 /**
349 * Returns true if recording is enabled.
350 */
351 int recordingEnabled()
352 {
Steve Block3856b092011-10-20 11:56:00 +0100353 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700354 if (mDevice->ops->recording_enabled)
355 return mDevice->ops->recording_enabled(mDevice);
356 return false;
357 }
358
359 /**
360 * Release a record frame previously returned by CAMERA_MSG_VIDEO_FRAME.
361 *
362 * It is camera hal client's responsibility to release video recording
363 * frames sent out by the camera hal before the camera hal receives
364 * a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME). After it receives
365 * the call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's
366 * responsibility of managing the life-cycle of the video recording
367 * frames.
368 */
369 void releaseRecordingFrame(const sp<IMemory>& mem)
370 {
Steve Block3856b092011-10-20 11:56:00 +0100371 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700372 if (mDevice->ops->release_recording_frame) {
373 ssize_t offset;
374 size_t size;
375 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
376 void *data = ((uint8_t *)heap->base()) + offset;
377 return mDevice->ops->release_recording_frame(mDevice, data);
378 }
379 }
380
381 /**
382 * Start auto focus, the notification callback routine is called
383 * with CAMERA_MSG_FOCUS once when focusing is complete. autoFocus()
384 * will be called again if another auto focus is needed.
385 */
386 status_t autoFocus()
387 {
Steve Block3856b092011-10-20 11:56:00 +0100388 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700389 if (mDevice->ops->auto_focus)
390 return mDevice->ops->auto_focus(mDevice);
391 return INVALID_OPERATION;
392 }
393
394 /**
395 * Cancels auto-focus function. If the auto-focus is still in progress,
396 * this function will cancel it. Whether the auto-focus is in progress
397 * or not, this function will return the focus position to the default.
398 * If the camera does not support auto-focus, this is a no-op.
399 */
400 status_t cancelAutoFocus()
401 {
Steve Block3856b092011-10-20 11:56:00 +0100402 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700403 if (mDevice->ops->cancel_auto_focus)
404 return mDevice->ops->cancel_auto_focus(mDevice);
405 return INVALID_OPERATION;
406 }
407
408 /**
409 * Take a picture.
410 */
411 status_t takePicture()
412 {
Steve Block3856b092011-10-20 11:56:00 +0100413 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700414 if (mDevice->ops->take_picture)
415 return mDevice->ops->take_picture(mDevice);
416 return INVALID_OPERATION;
417 }
418
419 /**
420 * Cancel a picture that was started with takePicture. Calling this
421 * method when no picture is being taken is a no-op.
422 */
423 status_t cancelPicture()
424 {
Steve Block3856b092011-10-20 11:56:00 +0100425 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700426 if (mDevice->ops->cancel_picture)
427 return mDevice->ops->cancel_picture(mDevice);
428 return INVALID_OPERATION;
429 }
430
431 /**
432 * Set the camera parameters. This returns BAD_VALUE if any parameter is
433 * invalid or not supported. */
434 status_t setParameters(const CameraParameters &params)
435 {
Steve Block3856b092011-10-20 11:56:00 +0100436 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700437 if (mDevice->ops->set_parameters)
438 return mDevice->ops->set_parameters(mDevice,
439 params.flatten().string());
440 return INVALID_OPERATION;
441 }
442
443 /** Return the camera parameters. */
444 CameraParameters getParameters() const
445 {
Steve Block3856b092011-10-20 11:56:00 +0100446 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700447 CameraParameters parms;
448 if (mDevice->ops->get_parameters) {
449 char *temp = mDevice->ops->get_parameters(mDevice);
450 String8 str_parms(temp);
Iliyan Malchev85fb61e2011-07-26 15:56:44 -0700451 if (mDevice->ops->put_parameters)
452 mDevice->ops->put_parameters(mDevice, temp);
453 else
454 free(temp);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700455 parms.unflatten(str_parms);
456 }
457 return parms;
458 }
459
460 /**
461 * Send command to camera driver.
462 */
463 status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
464 {
Steve Block3856b092011-10-20 11:56:00 +0100465 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700466 if (mDevice->ops->send_command)
467 return mDevice->ops->send_command(mDevice, cmd, arg1, arg2);
468 return INVALID_OPERATION;
469 }
470
471 /**
472 * Release the hardware resources owned by this object. Note that this is
473 * *not* done in the destructor.
474 */
475 void release() {
Steve Block3856b092011-10-20 11:56:00 +0100476 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700477 if (mDevice->ops->release)
478 mDevice->ops->release(mDevice);
479 }
480
481 /**
482 * Dump state of the camera hardware
483 */
Igor Murashkinddf3c502012-10-12 16:56:11 -0700484 status_t dump(int fd, const Vector<String16>& /*args*/) const
Iliyan Malchev8951a972011-04-14 16:55:59 -0700485 {
Steve Block3856b092011-10-20 11:56:00 +0100486 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700487 if (mDevice->ops->dump)
488 return mDevice->ops->dump(mDevice, fd);
489 return OK; // It's fine if the HAL doesn't implement dump()
490 }
491
492private:
493 camera_device_t *mDevice;
494 String8 mName;
495
496 static void __notify_cb(int32_t msg_type, int32_t ext1,
497 int32_t ext2, void *user)
498 {
Steve Block3856b092011-10-20 11:56:00 +0100499 ALOGV("%s", __FUNCTION__);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700500 CameraHardwareInterface *__this =
501 static_cast<CameraHardwareInterface *>(user);
502 __this->mNotifyCb(msg_type, ext1, ext2, __this->mCbUser);
503 }
504
505 static void __data_cb(int32_t msg_type,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700506 const camera_memory_t *data, unsigned int index,
Wu-cheng Liff09ef82011-07-28 05:30:59 +0800507 camera_frame_metadata_t *metadata,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700508 void *user)
509 {
Steve Block3856b092011-10-20 11:56:00 +0100510 ALOGV("%s", __FUNCTION__);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700511 CameraHardwareInterface *__this =
512 static_cast<CameraHardwareInterface *>(user);
513 sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(data->handle));
Iliyan Malchev26adde82011-06-06 17:21:32 -0700514 if (index >= mem->mNumBufs) {
Steve Block29357bc2012-01-06 19:20:56 +0000515 ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700516 index, mem->mNumBufs);
517 return;
518 }
Wu-cheng Liff09ef82011-07-28 05:30:59 +0800519 __this->mDataCb(msg_type, mem->mBuffers[index], metadata, __this->mCbUser);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700520 }
521
522 static void __data_cb_timestamp(nsecs_t timestamp, int32_t msg_type,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700523 const camera_memory_t *data, unsigned index,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700524 void *user)
525 {
Steve Block3856b092011-10-20 11:56:00 +0100526 ALOGV("%s", __FUNCTION__);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700527 CameraHardwareInterface *__this =
528 static_cast<CameraHardwareInterface *>(user);
529 // Start refcounting the heap object from here on. When the clients
530 // drop all references, it will be destroyed (as well as the enclosed
531 // MemoryHeapBase.
532 sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(data->handle));
Iliyan Malchev26adde82011-06-06 17:21:32 -0700533 if (index >= mem->mNumBufs) {
Steve Block29357bc2012-01-06 19:20:56 +0000534 ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700535 index, mem->mNumBufs);
536 return;
537 }
538 __this->mDataCbTimestamp(timestamp, msg_type, mem->mBuffers[index], __this->mCbUser);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700539 }
540
541 // This is a utility class that combines a MemoryHeapBase and a MemoryBase
542 // in one. Since we tend to use them in a one-to-one relationship, this is
543 // handy.
544
Iliyan Malchev26adde82011-06-06 17:21:32 -0700545 class CameraHeapMemory : public RefBase {
Iliyan Malchev8951a972011-04-14 16:55:59 -0700546 public:
Iliyan Malchev26adde82011-06-06 17:21:32 -0700547 CameraHeapMemory(int fd, size_t buf_size, uint_t num_buffers = 1) :
548 mBufSize(buf_size),
549 mNumBufs(num_buffers)
Iliyan Malchev8951a972011-04-14 16:55:59 -0700550 {
Iliyan Malchev26adde82011-06-06 17:21:32 -0700551 mHeap = new MemoryHeapBase(fd, buf_size * num_buffers);
552 commonInitialization();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700553 }
554
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -0700555 explicit CameraHeapMemory(size_t buf_size, uint_t num_buffers = 1) :
556 mBufSize(buf_size),
557 mNumBufs(num_buffers)
Iliyan Malchev26adde82011-06-06 17:21:32 -0700558 {
559 mHeap = new MemoryHeapBase(buf_size * num_buffers);
560 commonInitialization();
561 }
562
563 void commonInitialization()
564 {
565 handle.data = mHeap->base();
566 handle.size = mBufSize * mNumBufs;
567 handle.handle = this;
568
569 mBuffers = new sp<MemoryBase>[mNumBufs];
570 for (uint_t i = 0; i < mNumBufs; i++)
571 mBuffers[i] = new MemoryBase(mHeap,
572 i * mBufSize,
573 mBufSize);
574
575 handle.release = __put_memory;
576 }
577
578 virtual ~CameraHeapMemory()
579 {
580 delete [] mBuffers;
581 }
582
583 size_t mBufSize;
584 uint_t mNumBufs;
585 sp<MemoryHeapBase> mHeap;
586 sp<MemoryBase> *mBuffers;
587
Iliyan Malchev8951a972011-04-14 16:55:59 -0700588 camera_memory_t handle;
589 };
590
Iliyan Malchev26adde82011-06-06 17:21:32 -0700591 static camera_memory_t* __get_memory(int fd, size_t buf_size, uint_t num_bufs,
592 void *user __attribute__((unused)))
Iliyan Malchev8951a972011-04-14 16:55:59 -0700593 {
Iliyan Malchev26adde82011-06-06 17:21:32 -0700594 CameraHeapMemory *mem;
595 if (fd < 0)
596 mem = new CameraHeapMemory(buf_size, num_bufs);
597 else
598 mem = new CameraHeapMemory(fd, buf_size, num_bufs);
599 mem->incStrong(mem);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700600 return &mem->handle;
601 }
602
Iliyan Malchev26adde82011-06-06 17:21:32 -0700603 static void __put_memory(camera_memory_t *data)
604 {
605 if (!data)
606 return;
607
608 CameraHeapMemory *mem = static_cast<CameraHeapMemory *>(data->handle);
609 mem->decStrong(mem);
610 }
611
Iliyan Malchev8951a972011-04-14 16:55:59 -0700612 static ANativeWindow *__to_anw(void *user)
613 {
614 CameraHardwareInterface *__this =
615 reinterpret_cast<CameraHardwareInterface *>(user);
616 return __this->mPreviewWindow.get();
617 }
Chih-Hung Hsiehbf291732016-05-17 15:16:07 -0700618#define anw(n) __to_anw(((struct camera_preview_window *)(n))->user)
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800619#define hwi(n) reinterpret_cast<CameraHardwareInterface *>(\
Chih-Hung Hsieh4d1522b2016-05-24 11:32:12 -0700620 ((struct camera_preview_window *)(n))->user)
Iliyan Malchev8951a972011-04-14 16:55:59 -0700621
622 static int __dequeue_buffer(struct preview_stream_ops* w,
Iliyan Malchevafcedc92011-06-10 16:05:23 -0700623 buffer_handle_t** buffer, int *stride)
Iliyan Malchev8951a972011-04-14 16:55:59 -0700624 {
625 int rc;
626 ANativeWindow *a = anw(w);
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700627 ANativeWindowBuffer* anb;
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700628 rc = native_window_dequeue_buffer_and_wait(a, &anb);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700629 if (!rc) {
Sundar Raman1e06f432011-06-17 09:05:09 -0500630 *buffer = &anb->handle;
631 *stride = anb->stride;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700632 }
633 return rc;
634 }
635
636#ifndef container_of
637#define container_of(ptr, type, member) ({ \
Dan Albert36802bd2014-11-20 11:31:17 -0800638 const __typeof__(((type *) 0)->member) *__mptr = (ptr); \
Iliyan Malchev8951a972011-04-14 16:55:59 -0700639 (type *) ((char *) __mptr - (char *)(&((type *)0)->member)); })
640#endif
641
Sundar Raman1e06f432011-06-17 09:05:09 -0500642 static int __lock_buffer(struct preview_stream_ops* w,
Igor Murashkinddf3c502012-10-12 16:56:11 -0700643 buffer_handle_t* /*buffer*/)
Sundar Raman1e06f432011-06-17 09:05:09 -0500644 {
645 ANativeWindow *a = anw(w);
Igor Murashkinddf3c502012-10-12 16:56:11 -0700646 (void)a;
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700647 return 0;
Sundar Raman1e06f432011-06-17 09:05:09 -0500648 }
649
Iliyan Malchev8951a972011-04-14 16:55:59 -0700650 static int __enqueue_buffer(struct preview_stream_ops* w,
651 buffer_handle_t* buffer)
652 {
653 ANativeWindow *a = anw(w);
654 return a->queueBuffer(a,
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700655 container_of(buffer, ANativeWindowBuffer, handle), -1);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700656 }
657
658 static int __cancel_buffer(struct preview_stream_ops* w,
659 buffer_handle_t* buffer)
660 {
661 ANativeWindow *a = anw(w);
662 return a->cancelBuffer(a,
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700663 container_of(buffer, ANativeWindowBuffer, handle), -1);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700664 }
665
666 static int __set_buffer_count(struct preview_stream_ops* w, int count)
667 {
668 ANativeWindow *a = anw(w);
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800669
670 if (a != nullptr) {
671 // Workaround for b/27039775
672 // Previously, setting the buffer count would reset the buffer
673 // queue's flag that allows for all buffers to be dequeued on the
674 // producer side, instead of just the producer's declared max count,
675 // if no filled buffers have yet been queued by the producer. This
676 // reset no longer happens, but some HALs depend on this behavior,
677 // so it needs to be maintained for HAL backwards compatibility.
678 // Simulate the prior behavior by disconnecting/reconnecting to the
679 // window and setting the values again. This has the drawback of
680 // actually causing memory reallocation, which may not have happened
681 // in the past.
682 CameraHardwareInterface *hw = hwi(w);
683 native_window_api_disconnect(a, NATIVE_WINDOW_API_CAMERA);
684 native_window_api_connect(a, NATIVE_WINDOW_API_CAMERA);
685 if (hw->mPreviewScalingMode != NOT_SET) {
686 native_window_set_scaling_mode(a, hw->mPreviewScalingMode);
687 }
688 if (hw->mPreviewTransform != NOT_SET) {
689 native_window_set_buffers_transform(a, hw->mPreviewTransform);
690 }
691 if (hw->mPreviewWidth != NOT_SET) {
692 native_window_set_buffers_dimensions(a,
693 hw->mPreviewWidth, hw->mPreviewHeight);
694 native_window_set_buffers_format(a, hw->mPreviewFormat);
695 }
696 if (hw->mPreviewUsage != 0) {
697 native_window_set_usage(a, hw->mPreviewUsage);
698 }
699 if (hw->mPreviewSwapInterval != NOT_SET) {
700 a->setSwapInterval(a, hw->mPreviewSwapInterval);
701 }
702 if (hw->mPreviewCrop.left != NOT_SET) {
703 native_window_set_crop(a, &(hw->mPreviewCrop));
704 }
705 }
706
Iliyan Malchev26adde82011-06-06 17:21:32 -0700707 return native_window_set_buffer_count(a, count);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700708 }
709
710 static int __set_buffers_geometry(struct preview_stream_ops* w,
711 int width, int height, int format)
712 {
Pierre Couillaudc09cec72014-07-03 10:55:00 -0700713 int rc;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700714 ANativeWindow *a = anw(w);
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800715 CameraHardwareInterface *hw = hwi(w);
716 hw->mPreviewWidth = width;
717 hw->mPreviewHeight = height;
718 hw->mPreviewFormat = format;
Pierre Couillaudc09cec72014-07-03 10:55:00 -0700719 rc = native_window_set_buffers_dimensions(a, width, height);
720 if (!rc) {
721 rc = native_window_set_buffers_format(a, format);
722 }
723 return rc;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700724 }
725
726 static int __set_crop(struct preview_stream_ops *w,
727 int left, int top, int right, int bottom)
728 {
729 ANativeWindow *a = anw(w);
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800730 CameraHardwareInterface *hw = hwi(w);
731 hw->mPreviewCrop.left = left;
732 hw->mPreviewCrop.top = top;
733 hw->mPreviewCrop.right = right;
734 hw->mPreviewCrop.bottom = bottom;
735 return native_window_set_crop(a, &(hw->mPreviewCrop));
Iliyan Malchev8951a972011-04-14 16:55:59 -0700736 }
737
Eino-Ville Talvala9f3d5912011-07-26 14:06:07 -0700738 static int __set_timestamp(struct preview_stream_ops *w,
739 int64_t timestamp) {
740 ANativeWindow *a = anw(w);
741 return native_window_set_buffers_timestamp(a, timestamp);
742 }
743
Iliyan Malchev8951a972011-04-14 16:55:59 -0700744 static int __set_usage(struct preview_stream_ops* w, int usage)
745 {
746 ANativeWindow *a = anw(w);
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800747 CameraHardwareInterface *hw = hwi(w);
748 hw->mPreviewUsage = usage;
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700749 return native_window_set_usage(a, usage);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700750 }
751
752 static int __set_swap_interval(struct preview_stream_ops *w, int interval)
753 {
754 ANativeWindow *a = anw(w);
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800755 CameraHardwareInterface *hw = hwi(w);
756 hw->mPreviewSwapInterval = interval;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700757 return a->setSwapInterval(a, interval);
758 }
759
760 static int __get_min_undequeued_buffer_count(
761 const struct preview_stream_ops *w,
762 int *count)
763 {
764 ANativeWindow *a = anw(w);
765 return a->query(a, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, count);
766 }
767
768 void initHalPreviewWindow()
769 {
770 mHalPreviewWindow.nw.cancel_buffer = __cancel_buffer;
Sundar Raman1e06f432011-06-17 09:05:09 -0500771 mHalPreviewWindow.nw.lock_buffer = __lock_buffer;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700772 mHalPreviewWindow.nw.dequeue_buffer = __dequeue_buffer;
773 mHalPreviewWindow.nw.enqueue_buffer = __enqueue_buffer;
774 mHalPreviewWindow.nw.set_buffer_count = __set_buffer_count;
775 mHalPreviewWindow.nw.set_buffers_geometry = __set_buffers_geometry;
776 mHalPreviewWindow.nw.set_crop = __set_crop;
Eino-Ville Talvala9f3d5912011-07-26 14:06:07 -0700777 mHalPreviewWindow.nw.set_timestamp = __set_timestamp;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700778 mHalPreviewWindow.nw.set_usage = __set_usage;
779 mHalPreviewWindow.nw.set_swap_interval = __set_swap_interval;
780
781 mHalPreviewWindow.nw.get_min_undequeued_buffer_count =
782 __get_min_undequeued_buffer_count;
783 }
784
785 sp<ANativeWindow> mPreviewWindow;
786
787 struct camera_preview_window {
788 struct preview_stream_ops nw;
789 void *user;
790 };
791
792 struct camera_preview_window mHalPreviewWindow;
793
794 notify_callback mNotifyCb;
795 data_callback mDataCb;
796 data_callback_timestamp mDataCbTimestamp;
797 void *mCbUser;
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800798
799 // Cached values for preview stream parameters
800 static const int NOT_SET = -1;
801 int mPreviewScalingMode;
802 int mPreviewTransform;
803 int mPreviewWidth;
804 int mPreviewHeight;
805 int mPreviewFormat;
806 int mPreviewUsage;
807 int mPreviewSwapInterval;
808 android_native_rect_t mPreviewCrop;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700809};
810
811}; // namespace android
812
813#endif