blob: 0fe76e57e3d5ec2fb4a33d84820d92a4bb419e2d [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);
Zhijun Heb10cdad2014-06-16 16:38:35 -0700105 if (res != OK) return res;
106
107 int rc = OK;
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800108 if (module->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_3 &&
Zhijun Heb10cdad2014-06-16 16:38:35 -0700109 info.device_version > CAMERA_DEVICE_API_VERSION_1_0) {
110 // Open higher version camera device as HAL1.0 device.
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800111 rc = module->openLegacy(mName.string(),
112 CAMERA_DEVICE_API_VERSION_1_0,
113 (hw_device_t **)&mDevice);
Zhijun Heb10cdad2014-06-16 16:38:35 -0700114 } else {
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800115 rc = module->open(mName.string(), (hw_device_t **)&mDevice);
Zhijun Heb10cdad2014-06-16 16:38:35 -0700116 }
Tyler Luu5861a9a2011-10-06 00:00:03 -0500117 if (rc != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000118 ALOGE("Could not open camera %s: %d", mName.string(), rc);
Tyler Luu5861a9a2011-10-06 00:00:03 -0500119 return rc;
120 }
121 initHalPreviewWindow();
122 return rc;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700123 }
124
125 /** Set the ANativeWindow to which preview frames are sent */
126 status_t setPreviewWindow(const sp<ANativeWindow>& buf)
127 {
Steve Block3856b092011-10-20 11:56:00 +0100128 ALOGV("%s(%s) buf %p", __FUNCTION__, mName.string(), buf.get());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700129 if (mDevice->ops->set_preview_window) {
130 mPreviewWindow = buf;
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800131 if (buf != nullptr) {
132 if (mPreviewScalingMode != NOT_SET) {
133 setPreviewScalingMode(mPreviewScalingMode);
134 }
135 if (mPreviewTransform != NOT_SET) {
136 setPreviewTransform(mPreviewTransform);
137 }
138 }
Iliyan Malchev8951a972011-04-14 16:55:59 -0700139 mHalPreviewWindow.user = this;
Steve Block3856b092011-10-20 11:56:00 +0100140 ALOGV("%s &mHalPreviewWindow %p mHalPreviewWindow.user %p", __FUNCTION__,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700141 &mHalPreviewWindow, mHalPreviewWindow.user);
142 return mDevice->ops->set_preview_window(mDevice,
143 buf.get() ? &mHalPreviewWindow.nw : 0);
144 }
145 return INVALID_OPERATION;
146 }
147
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800148 status_t setPreviewScalingMode(int scalingMode)
149 {
150 int rc = OK;
151 mPreviewScalingMode = scalingMode;
152 if (mPreviewWindow != nullptr) {
153 rc = native_window_set_scaling_mode(mPreviewWindow.get(),
154 scalingMode);
155 }
156 return rc;
157 }
158
159 status_t setPreviewTransform(int transform) {
160 int rc = OK;
161 mPreviewTransform = transform;
162 if (mPreviewWindow != nullptr) {
163 rc = native_window_set_buffers_transform(mPreviewWindow.get(),
164 mPreviewTransform);
165 }
166 return rc;
167 }
168
Iliyan Malchev8951a972011-04-14 16:55:59 -0700169 /** Set the notification and data callbacks */
170 void setCallbacks(notify_callback notify_cb,
171 data_callback data_cb,
172 data_callback_timestamp data_cb_timestamp,
173 void* user)
174 {
175 mNotifyCb = notify_cb;
176 mDataCb = data_cb;
177 mDataCbTimestamp = data_cb_timestamp;
178 mCbUser = user;
179
Steve Block3856b092011-10-20 11:56:00 +0100180 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700181
182 if (mDevice->ops->set_callbacks) {
183 mDevice->ops->set_callbacks(mDevice,
184 __notify_cb,
185 __data_cb,
186 __data_cb_timestamp,
187 __get_memory,
188 this);
189 }
190 }
191
192 /**
193 * The following three functions all take a msgtype,
194 * which is a bitmask of the messages defined in
195 * include/ui/Camera.h
196 */
197
198 /**
199 * Enable a message, or set of messages.
200 */
201 void enableMsgType(int32_t msgType)
202 {
Steve Block3856b092011-10-20 11:56:00 +0100203 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700204 if (mDevice->ops->enable_msg_type)
205 mDevice->ops->enable_msg_type(mDevice, msgType);
206 }
207
208 /**
209 * Disable a message, or a set of messages.
210 *
211 * Once received a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), camera hal
212 * should not rely on its client to call releaseRecordingFrame() to release
213 * video recording frames sent out by the cameral hal before and after the
214 * disableMsgType(CAMERA_MSG_VIDEO_FRAME) call. Camera hal clients must not
215 * modify/access any video recording frame after calling
216 * disableMsgType(CAMERA_MSG_VIDEO_FRAME).
217 */
218 void disableMsgType(int32_t msgType)
219 {
Steve Block3856b092011-10-20 11:56:00 +0100220 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700221 if (mDevice->ops->disable_msg_type)
222 mDevice->ops->disable_msg_type(mDevice, msgType);
223 }
224
225 /**
226 * Query whether a message, or a set of messages, is enabled.
227 * Note that this is operates as an AND, if any of the messages
228 * queried are off, this will return false.
229 */
230 int msgTypeEnabled(int32_t msgType)
231 {
Steve Block3856b092011-10-20 11:56:00 +0100232 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700233 if (mDevice->ops->msg_type_enabled)
234 return mDevice->ops->msg_type_enabled(mDevice, msgType);
235 return false;
236 }
237
238 /**
239 * Start preview mode.
240 */
241 status_t startPreview()
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->start_preview)
245 return mDevice->ops->start_preview(mDevice);
246 return INVALID_OPERATION;
247 }
248
249 /**
250 * Stop a previously started preview.
251 */
252 void stopPreview()
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->stop_preview)
256 mDevice->ops->stop_preview(mDevice);
257 }
258
259 /**
260 * Returns true if preview is enabled.
261 */
262 int previewEnabled()
263 {
Steve Block3856b092011-10-20 11:56:00 +0100264 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700265 if (mDevice->ops->preview_enabled)
266 return mDevice->ops->preview_enabled(mDevice);
267 return false;
268 }
269
270 /**
271 * Request the camera hal to store meta data or real YUV data in
272 * the video buffers send out via CAMERA_MSG_VIDEO_FRRAME for a
273 * recording session. If it is not called, the default camera
274 * hal behavior is to store real YUV data in the video buffers.
275 *
276 * This method should be called before startRecording() in order
277 * to be effective.
278 *
279 * If meta data is stored in the video buffers, it is up to the
280 * receiver of the video buffers to interpret the contents and
281 * to find the actual frame data with the help of the meta data
282 * in the buffer. How this is done is outside of the scope of
283 * this method.
284 *
285 * Some camera hal may not support storing meta data in the video
286 * buffers, but all camera hal should support storing real YUV data
287 * in the video buffers. If the camera hal does not support storing
288 * the meta data in the video buffers when it is requested to do
289 * do, INVALID_OPERATION must be returned. It is very useful for
290 * the camera hal to pass meta data rather than the actual frame
291 * data directly to the video encoder, since the amount of the
292 * uncompressed frame data can be very large if video size is large.
293 *
294 * @param enable if true to instruct the camera hal to store
295 * meta data in the video buffers; false to instruct
296 * the camera hal to store real YUV data in the video
297 * buffers.
298 *
299 * @return OK on success.
300 */
301
302 status_t storeMetaDataInBuffers(int enable)
303 {
Steve Block3856b092011-10-20 11:56:00 +0100304 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700305 if (mDevice->ops->store_meta_data_in_buffers)
306 return mDevice->ops->store_meta_data_in_buffers(mDevice, enable);
307 return enable ? INVALID_OPERATION: OK;
308 }
309
310 /**
311 * Start record mode. When a record image is available a CAMERA_MSG_VIDEO_FRAME
312 * message is sent with the corresponding frame. Every record frame must be released
313 * by a cameral hal client via releaseRecordingFrame() before the client calls
314 * disableMsgType(CAMERA_MSG_VIDEO_FRAME). After the client calls
315 * disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's responsibility
316 * to manage the life-cycle of the video recording frames, and the client must
317 * not modify/access any video recording frames.
318 */
319 status_t startRecording()
320 {
Steve Block3856b092011-10-20 11:56:00 +0100321 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700322 if (mDevice->ops->start_recording)
323 return mDevice->ops->start_recording(mDevice);
324 return INVALID_OPERATION;
325 }
326
327 /**
328 * Stop a previously started recording.
329 */
330 void stopRecording()
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->stop_recording)
334 mDevice->ops->stop_recording(mDevice);
335 }
336
337 /**
338 * Returns true if recording is enabled.
339 */
340 int recordingEnabled()
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->recording_enabled)
344 return mDevice->ops->recording_enabled(mDevice);
345 return false;
346 }
347
348 /**
349 * Release a record frame previously returned by CAMERA_MSG_VIDEO_FRAME.
350 *
351 * It is camera hal client's responsibility to release video recording
352 * frames sent out by the camera hal before the camera hal receives
353 * a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME). After it receives
354 * the call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is camera hal's
355 * responsibility of managing the life-cycle of the video recording
356 * frames.
357 */
358 void releaseRecordingFrame(const sp<IMemory>& mem)
359 {
Steve Block3856b092011-10-20 11:56:00 +0100360 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700361 if (mDevice->ops->release_recording_frame) {
362 ssize_t offset;
363 size_t size;
364 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
365 void *data = ((uint8_t *)heap->base()) + offset;
366 return mDevice->ops->release_recording_frame(mDevice, data);
367 }
368 }
369
370 /**
371 * Start auto focus, the notification callback routine is called
372 * with CAMERA_MSG_FOCUS once when focusing is complete. autoFocus()
373 * will be called again if another auto focus is needed.
374 */
375 status_t autoFocus()
376 {
Steve Block3856b092011-10-20 11:56:00 +0100377 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700378 if (mDevice->ops->auto_focus)
379 return mDevice->ops->auto_focus(mDevice);
380 return INVALID_OPERATION;
381 }
382
383 /**
384 * Cancels auto-focus function. If the auto-focus is still in progress,
385 * this function will cancel it. Whether the auto-focus is in progress
386 * or not, this function will return the focus position to the default.
387 * If the camera does not support auto-focus, this is a no-op.
388 */
389 status_t cancelAutoFocus()
390 {
Steve Block3856b092011-10-20 11:56:00 +0100391 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700392 if (mDevice->ops->cancel_auto_focus)
393 return mDevice->ops->cancel_auto_focus(mDevice);
394 return INVALID_OPERATION;
395 }
396
397 /**
398 * Take a picture.
399 */
400 status_t takePicture()
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->take_picture)
404 return mDevice->ops->take_picture(mDevice);
405 return INVALID_OPERATION;
406 }
407
408 /**
409 * Cancel a picture that was started with takePicture. Calling this
410 * method when no picture is being taken is a no-op.
411 */
412 status_t cancelPicture()
413 {
Steve Block3856b092011-10-20 11:56:00 +0100414 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700415 if (mDevice->ops->cancel_picture)
416 return mDevice->ops->cancel_picture(mDevice);
417 return INVALID_OPERATION;
418 }
419
420 /**
421 * Set the camera parameters. This returns BAD_VALUE if any parameter is
422 * invalid or not supported. */
423 status_t setParameters(const CameraParameters &params)
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->set_parameters)
427 return mDevice->ops->set_parameters(mDevice,
428 params.flatten().string());
429 return INVALID_OPERATION;
430 }
431
432 /** Return the camera parameters. */
433 CameraParameters getParameters() const
434 {
Steve Block3856b092011-10-20 11:56:00 +0100435 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700436 CameraParameters parms;
437 if (mDevice->ops->get_parameters) {
438 char *temp = mDevice->ops->get_parameters(mDevice);
439 String8 str_parms(temp);
Iliyan Malchev85fb61e2011-07-26 15:56:44 -0700440 if (mDevice->ops->put_parameters)
441 mDevice->ops->put_parameters(mDevice, temp);
442 else
443 free(temp);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700444 parms.unflatten(str_parms);
445 }
446 return parms;
447 }
448
449 /**
450 * Send command to camera driver.
451 */
452 status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
453 {
Steve Block3856b092011-10-20 11:56:00 +0100454 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700455 if (mDevice->ops->send_command)
456 return mDevice->ops->send_command(mDevice, cmd, arg1, arg2);
457 return INVALID_OPERATION;
458 }
459
460 /**
461 * Release the hardware resources owned by this object. Note that this is
462 * *not* done in the destructor.
463 */
464 void release() {
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->release)
467 mDevice->ops->release(mDevice);
468 }
469
470 /**
471 * Dump state of the camera hardware
472 */
Igor Murashkinddf3c502012-10-12 16:56:11 -0700473 status_t dump(int fd, const Vector<String16>& /*args*/) const
Iliyan Malchev8951a972011-04-14 16:55:59 -0700474 {
Steve Block3856b092011-10-20 11:56:00 +0100475 ALOGV("%s(%s)", __FUNCTION__, mName.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700476 if (mDevice->ops->dump)
477 return mDevice->ops->dump(mDevice, fd);
478 return OK; // It's fine if the HAL doesn't implement dump()
479 }
480
481private:
482 camera_device_t *mDevice;
483 String8 mName;
484
485 static void __notify_cb(int32_t msg_type, int32_t ext1,
486 int32_t ext2, void *user)
487 {
Steve Block3856b092011-10-20 11:56:00 +0100488 ALOGV("%s", __FUNCTION__);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700489 CameraHardwareInterface *__this =
490 static_cast<CameraHardwareInterface *>(user);
491 __this->mNotifyCb(msg_type, ext1, ext2, __this->mCbUser);
492 }
493
494 static void __data_cb(int32_t msg_type,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700495 const camera_memory_t *data, unsigned int index,
Wu-cheng Liff09ef82011-07-28 05:30:59 +0800496 camera_frame_metadata_t *metadata,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700497 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 sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(data->handle));
Iliyan Malchev26adde82011-06-06 17:21:32 -0700503 if (index >= mem->mNumBufs) {
Steve Block29357bc2012-01-06 19:20:56 +0000504 ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700505 index, mem->mNumBufs);
506 return;
507 }
Wu-cheng Liff09ef82011-07-28 05:30:59 +0800508 __this->mDataCb(msg_type, mem->mBuffers[index], metadata, __this->mCbUser);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700509 }
510
511 static void __data_cb_timestamp(nsecs_t timestamp, int32_t msg_type,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700512 const camera_memory_t *data, unsigned index,
Iliyan Malchev8951a972011-04-14 16:55:59 -0700513 void *user)
514 {
Steve Block3856b092011-10-20 11:56:00 +0100515 ALOGV("%s", __FUNCTION__);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700516 CameraHardwareInterface *__this =
517 static_cast<CameraHardwareInterface *>(user);
518 // Start refcounting the heap object from here on. When the clients
519 // drop all references, it will be destroyed (as well as the enclosed
520 // MemoryHeapBase.
521 sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory *>(data->handle));
Iliyan Malchev26adde82011-06-06 17:21:32 -0700522 if (index >= mem->mNumBufs) {
Steve Block29357bc2012-01-06 19:20:56 +0000523 ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__,
Iliyan Malchev26adde82011-06-06 17:21:32 -0700524 index, mem->mNumBufs);
525 return;
526 }
527 __this->mDataCbTimestamp(timestamp, msg_type, mem->mBuffers[index], __this->mCbUser);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700528 }
529
530 // This is a utility class that combines a MemoryHeapBase and a MemoryBase
531 // in one. Since we tend to use them in a one-to-one relationship, this is
532 // handy.
533
Iliyan Malchev26adde82011-06-06 17:21:32 -0700534 class CameraHeapMemory : public RefBase {
Iliyan Malchev8951a972011-04-14 16:55:59 -0700535 public:
Iliyan Malchev26adde82011-06-06 17:21:32 -0700536 CameraHeapMemory(int fd, size_t buf_size, uint_t num_buffers = 1) :
537 mBufSize(buf_size),
538 mNumBufs(num_buffers)
Iliyan Malchev8951a972011-04-14 16:55:59 -0700539 {
Iliyan Malchev26adde82011-06-06 17:21:32 -0700540 mHeap = new MemoryHeapBase(fd, buf_size * num_buffers);
541 commonInitialization();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700542 }
543
Iliyan Malchev26adde82011-06-06 17:21:32 -0700544 CameraHeapMemory(size_t buf_size, uint_t num_buffers = 1) :
545 mBufSize(buf_size),
546 mNumBufs(num_buffers)
547 {
548 mHeap = new MemoryHeapBase(buf_size * num_buffers);
549 commonInitialization();
550 }
551
552 void commonInitialization()
553 {
554 handle.data = mHeap->base();
555 handle.size = mBufSize * mNumBufs;
556 handle.handle = this;
557
558 mBuffers = new sp<MemoryBase>[mNumBufs];
559 for (uint_t i = 0; i < mNumBufs; i++)
560 mBuffers[i] = new MemoryBase(mHeap,
561 i * mBufSize,
562 mBufSize);
563
564 handle.release = __put_memory;
565 }
566
567 virtual ~CameraHeapMemory()
568 {
569 delete [] mBuffers;
570 }
571
572 size_t mBufSize;
573 uint_t mNumBufs;
574 sp<MemoryHeapBase> mHeap;
575 sp<MemoryBase> *mBuffers;
576
Iliyan Malchev8951a972011-04-14 16:55:59 -0700577 camera_memory_t handle;
578 };
579
Iliyan Malchev26adde82011-06-06 17:21:32 -0700580 static camera_memory_t* __get_memory(int fd, size_t buf_size, uint_t num_bufs,
581 void *user __attribute__((unused)))
Iliyan Malchev8951a972011-04-14 16:55:59 -0700582 {
Iliyan Malchev26adde82011-06-06 17:21:32 -0700583 CameraHeapMemory *mem;
584 if (fd < 0)
585 mem = new CameraHeapMemory(buf_size, num_bufs);
586 else
587 mem = new CameraHeapMemory(fd, buf_size, num_bufs);
588 mem->incStrong(mem);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700589 return &mem->handle;
590 }
591
Iliyan Malchev26adde82011-06-06 17:21:32 -0700592 static void __put_memory(camera_memory_t *data)
593 {
594 if (!data)
595 return;
596
597 CameraHeapMemory *mem = static_cast<CameraHeapMemory *>(data->handle);
598 mem->decStrong(mem);
599 }
600
Iliyan Malchev8951a972011-04-14 16:55:59 -0700601 static ANativeWindow *__to_anw(void *user)
602 {
603 CameraHardwareInterface *__this =
604 reinterpret_cast<CameraHardwareInterface *>(user);
605 return __this->mPreviewWindow.get();
606 }
607#define anw(n) __to_anw(((struct camera_preview_window *)n)->user)
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800608#define hwi(n) reinterpret_cast<CameraHardwareInterface *>(\
609 ((struct camera_preview_window *)n)->user)
Iliyan Malchev8951a972011-04-14 16:55:59 -0700610
611 static int __dequeue_buffer(struct preview_stream_ops* w,
Iliyan Malchevafcedc92011-06-10 16:05:23 -0700612 buffer_handle_t** buffer, int *stride)
Iliyan Malchev8951a972011-04-14 16:55:59 -0700613 {
614 int rc;
615 ANativeWindow *a = anw(w);
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700616 ANativeWindowBuffer* anb;
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700617 rc = native_window_dequeue_buffer_and_wait(a, &anb);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700618 if (!rc) {
Sundar Raman1e06f432011-06-17 09:05:09 -0500619 *buffer = &anb->handle;
620 *stride = anb->stride;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700621 }
622 return rc;
623 }
624
625#ifndef container_of
626#define container_of(ptr, type, member) ({ \
Dan Albert36802bd2014-11-20 11:31:17 -0800627 const __typeof__(((type *) 0)->member) *__mptr = (ptr); \
Iliyan Malchev8951a972011-04-14 16:55:59 -0700628 (type *) ((char *) __mptr - (char *)(&((type *)0)->member)); })
629#endif
630
Sundar Raman1e06f432011-06-17 09:05:09 -0500631 static int __lock_buffer(struct preview_stream_ops* w,
Igor Murashkinddf3c502012-10-12 16:56:11 -0700632 buffer_handle_t* /*buffer*/)
Sundar Raman1e06f432011-06-17 09:05:09 -0500633 {
634 ANativeWindow *a = anw(w);
Igor Murashkinddf3c502012-10-12 16:56:11 -0700635 (void)a;
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700636 return 0;
Sundar Raman1e06f432011-06-17 09:05:09 -0500637 }
638
Iliyan Malchev8951a972011-04-14 16:55:59 -0700639 static int __enqueue_buffer(struct preview_stream_ops* w,
640 buffer_handle_t* buffer)
641 {
642 ANativeWindow *a = anw(w);
643 return a->queueBuffer(a,
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700644 container_of(buffer, ANativeWindowBuffer, handle), -1);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700645 }
646
647 static int __cancel_buffer(struct preview_stream_ops* w,
648 buffer_handle_t* buffer)
649 {
650 ANativeWindow *a = anw(w);
651 return a->cancelBuffer(a,
Jamie Gennis1e5b2b32012-06-13 16:29:51 -0700652 container_of(buffer, ANativeWindowBuffer, handle), -1);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700653 }
654
655 static int __set_buffer_count(struct preview_stream_ops* w, int count)
656 {
657 ANativeWindow *a = anw(w);
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800658
659 if (a != nullptr) {
660 // Workaround for b/27039775
661 // Previously, setting the buffer count would reset the buffer
662 // queue's flag that allows for all buffers to be dequeued on the
663 // producer side, instead of just the producer's declared max count,
664 // if no filled buffers have yet been queued by the producer. This
665 // reset no longer happens, but some HALs depend on this behavior,
666 // so it needs to be maintained for HAL backwards compatibility.
667 // Simulate the prior behavior by disconnecting/reconnecting to the
668 // window and setting the values again. This has the drawback of
669 // actually causing memory reallocation, which may not have happened
670 // in the past.
671 CameraHardwareInterface *hw = hwi(w);
672 native_window_api_disconnect(a, NATIVE_WINDOW_API_CAMERA);
673 native_window_api_connect(a, NATIVE_WINDOW_API_CAMERA);
674 if (hw->mPreviewScalingMode != NOT_SET) {
675 native_window_set_scaling_mode(a, hw->mPreviewScalingMode);
676 }
677 if (hw->mPreviewTransform != NOT_SET) {
678 native_window_set_buffers_transform(a, hw->mPreviewTransform);
679 }
680 if (hw->mPreviewWidth != NOT_SET) {
681 native_window_set_buffers_dimensions(a,
682 hw->mPreviewWidth, hw->mPreviewHeight);
683 native_window_set_buffers_format(a, hw->mPreviewFormat);
684 }
685 if (hw->mPreviewUsage != 0) {
686 native_window_set_usage(a, hw->mPreviewUsage);
687 }
688 if (hw->mPreviewSwapInterval != NOT_SET) {
689 a->setSwapInterval(a, hw->mPreviewSwapInterval);
690 }
691 if (hw->mPreviewCrop.left != NOT_SET) {
692 native_window_set_crop(a, &(hw->mPreviewCrop));
693 }
694 }
695
Iliyan Malchev26adde82011-06-06 17:21:32 -0700696 return native_window_set_buffer_count(a, count);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700697 }
698
699 static int __set_buffers_geometry(struct preview_stream_ops* w,
700 int width, int height, int format)
701 {
Pierre Couillaudc09cec72014-07-03 10:55:00 -0700702 int rc;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700703 ANativeWindow *a = anw(w);
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800704 CameraHardwareInterface *hw = hwi(w);
705 hw->mPreviewWidth = width;
706 hw->mPreviewHeight = height;
707 hw->mPreviewFormat = format;
Pierre Couillaudc09cec72014-07-03 10:55:00 -0700708 rc = native_window_set_buffers_dimensions(a, width, height);
709 if (!rc) {
710 rc = native_window_set_buffers_format(a, format);
711 }
712 return rc;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700713 }
714
715 static int __set_crop(struct preview_stream_ops *w,
716 int left, int top, int right, int bottom)
717 {
718 ANativeWindow *a = anw(w);
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800719 CameraHardwareInterface *hw = hwi(w);
720 hw->mPreviewCrop.left = left;
721 hw->mPreviewCrop.top = top;
722 hw->mPreviewCrop.right = right;
723 hw->mPreviewCrop.bottom = bottom;
724 return native_window_set_crop(a, &(hw->mPreviewCrop));
Iliyan Malchev8951a972011-04-14 16:55:59 -0700725 }
726
Eino-Ville Talvala9f3d5912011-07-26 14:06:07 -0700727 static int __set_timestamp(struct preview_stream_ops *w,
728 int64_t timestamp) {
729 ANativeWindow *a = anw(w);
730 return native_window_set_buffers_timestamp(a, timestamp);
731 }
732
Iliyan Malchev8951a972011-04-14 16:55:59 -0700733 static int __set_usage(struct preview_stream_ops* w, int usage)
734 {
735 ANativeWindow *a = anw(w);
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800736 CameraHardwareInterface *hw = hwi(w);
737 hw->mPreviewUsage = usage;
Iliyan Malchev8ce23642011-05-01 11:33:26 -0700738 return native_window_set_usage(a, usage);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700739 }
740
741 static int __set_swap_interval(struct preview_stream_ops *w, int interval)
742 {
743 ANativeWindow *a = anw(w);
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800744 CameraHardwareInterface *hw = hwi(w);
745 hw->mPreviewSwapInterval = interval;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700746 return a->setSwapInterval(a, interval);
747 }
748
749 static int __get_min_undequeued_buffer_count(
750 const struct preview_stream_ops *w,
751 int *count)
752 {
753 ANativeWindow *a = anw(w);
754 return a->query(a, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, count);
755 }
756
757 void initHalPreviewWindow()
758 {
759 mHalPreviewWindow.nw.cancel_buffer = __cancel_buffer;
Sundar Raman1e06f432011-06-17 09:05:09 -0500760 mHalPreviewWindow.nw.lock_buffer = __lock_buffer;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700761 mHalPreviewWindow.nw.dequeue_buffer = __dequeue_buffer;
762 mHalPreviewWindow.nw.enqueue_buffer = __enqueue_buffer;
763 mHalPreviewWindow.nw.set_buffer_count = __set_buffer_count;
764 mHalPreviewWindow.nw.set_buffers_geometry = __set_buffers_geometry;
765 mHalPreviewWindow.nw.set_crop = __set_crop;
Eino-Ville Talvala9f3d5912011-07-26 14:06:07 -0700766 mHalPreviewWindow.nw.set_timestamp = __set_timestamp;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700767 mHalPreviewWindow.nw.set_usage = __set_usage;
768 mHalPreviewWindow.nw.set_swap_interval = __set_swap_interval;
769
770 mHalPreviewWindow.nw.get_min_undequeued_buffer_count =
771 __get_min_undequeued_buffer_count;
772 }
773
774 sp<ANativeWindow> mPreviewWindow;
775
776 struct camera_preview_window {
777 struct preview_stream_ops nw;
778 void *user;
779 };
780
781 struct camera_preview_window mHalPreviewWindow;
782
783 notify_callback mNotifyCb;
784 data_callback mDataCb;
785 data_callback_timestamp mDataCbTimestamp;
786 void *mCbUser;
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800787
788 // Cached values for preview stream parameters
789 static const int NOT_SET = -1;
790 int mPreviewScalingMode;
791 int mPreviewTransform;
792 int mPreviewWidth;
793 int mPreviewHeight;
794 int mPreviewFormat;
795 int mPreviewUsage;
796 int mPreviewSwapInterval;
797 android_native_rect_t mPreviewCrop;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700798};
799
800}; // namespace android
801
802#endif