blob: bbcdf7ee283af95ff913224d6a2f8c82d1ed0001 [file] [log] [blame]
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001/*
2 * Copyright (C) 2016 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// Modified from hardware/libhardware/modules/camera/Camera.cpp
18
19#include <cstdlib>
20#include <stdio.h>
21#include <hardware/camera3.h>
22#include <sync/sync.h>
23#include <system/camera_metadata.h>
24#include <system/graphics.h>
25#include <utils/Mutex.h>
26#include "Stream.h"
27
28//#define LOG_NDEBUG 0
29#define LOG_TAG "Camera"
30#include <cutils/log.h>
31
32#define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
33#include <utils/Trace.h>
34
35#include "Camera.h"
36
37#define CAMERA_SYNC_TIMEOUT 5000 // in msecs
38
39namespace default_camera_hal {
40
41extern "C" {
42// Shim passed to the framework to close an opened device.
43static int close_device(hw_device_t* dev)
44{
45 camera3_device_t* cam_dev = reinterpret_cast<camera3_device_t*>(dev);
46 Camera* cam = static_cast<Camera*>(cam_dev->priv);
47 return cam->close();
48}
49} // extern "C"
50
51Camera::Camera(int id)
52 : mId(id),
53 mStaticInfo(NULL),
54 mBusy(false),
55 mCallbackOps(NULL),
56 mStreams(NULL),
57 mNumStreams(0),
58 mSettings(NULL)
59{
60 memset(&mTemplates, 0, sizeof(mTemplates));
61 memset(&mDevice, 0, sizeof(mDevice));
62 mDevice.common.tag = HARDWARE_DEVICE_TAG;
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070063 mDevice.common.version = CAMERA_DEVICE_API_VERSION_3_4;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070064 mDevice.common.close = close_device;
65 mDevice.ops = const_cast<camera3_device_ops_t*>(&sOps);
66 mDevice.priv = this;
67}
68
69Camera::~Camera()
70{
71 if (mStaticInfo != NULL) {
72 free_camera_metadata(mStaticInfo);
73 }
74 if (mSettings != NULL) {
75 free_camera_metadata(mSettings);
76 }
77 for (camera_metadata_t* metadata : mTemplates) {
78 if (metadata != NULL) {
79 free_camera_metadata(metadata);
80 }
81 }
82}
83
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070084int Camera::openDevice(const hw_module_t *module, hw_device_t **device)
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070085{
86 ALOGI("%s:%d: Opening camera device", __func__, mId);
87 ATRACE_CALL();
88 android::Mutex::Autolock al(mDeviceLock);
89
90 if (mBusy) {
91 ALOGE("%s:%d: Error! Camera device already opened", __func__, mId);
92 return -EBUSY;
93 }
94
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070095 int connectResult = connect();
96 if (connectResult != 0) {
97 return connectResult;
98 }
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070099 mBusy = true;
100 mDevice.common.module = const_cast<hw_module_t*>(module);
101 *device = &mDevice.common;
102 return 0;
103}
104
105int Camera::getInfo(struct camera_info *info)
106{
107 android::Mutex::Autolock al(mStaticInfoLock);
108
109 info->device_version = mDevice.common.version;
110 initDeviceInfo(info);
111 if (mStaticInfo == NULL) {
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700112 if (initStaticInfo(&mStaticInfo)) {
113 return -ENODEV;
114 }
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700115 }
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700116 info->static_camera_characteristics = mStaticInfo;
117 return 0;
118}
119
120int Camera::close()
121{
122 ALOGI("%s:%d: Closing camera device", __func__, mId);
123 ATRACE_CALL();
124 android::Mutex::Autolock al(mDeviceLock);
125
126 if (!mBusy) {
127 ALOGE("%s:%d: Error! Camera device not open", __func__, mId);
128 return -EINVAL;
129 }
130
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -0700131 disconnect();
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700132 mBusy = false;
133 return 0;
134}
135
136int Camera::initialize(const camera3_callback_ops_t *callback_ops)
137{
138 int res;
139
140 ALOGV("%s:%d: callback_ops=%p", __func__, mId, callback_ops);
141 mCallbackOps = callback_ops;
142 // per-device specific initialization
143 res = initDevice();
144 if (res != 0) {
145 ALOGE("%s:%d: Failed to initialize device!", __func__, mId);
146 return res;
147 }
148 return 0;
149}
150
151int Camera::configureStreams(camera3_stream_configuration_t *stream_config)
152{
153 camera3_stream_t *astream;
154 Stream **newStreams = NULL;
155
156 ALOGV("%s:%d: stream_config=%p", __func__, mId, stream_config);
157 ATRACE_CALL();
158 android::Mutex::Autolock al(mDeviceLock);
159
160 if (stream_config == NULL) {
161 ALOGE("%s:%d: NULL stream configuration array", __func__, mId);
162 return -EINVAL;
163 }
164 if (stream_config->num_streams == 0) {
165 ALOGE("%s:%d: Empty stream configuration array", __func__, mId);
166 return -EINVAL;
167 }
168
169 // Create new stream array
170 newStreams = new Stream*[stream_config->num_streams];
171 ALOGV("%s:%d: Number of Streams: %d", __func__, mId,
172 stream_config->num_streams);
173
174 // Mark all current streams unused for now
175 for (int i = 0; i < mNumStreams; i++)
176 mStreams[i]->mReuse = false;
177 // Fill new stream array with reused streams and new streams
178 for (unsigned int i = 0; i < stream_config->num_streams; i++) {
179 astream = stream_config->streams[i];
180 if (astream->max_buffers > 0) {
181 ALOGV("%s:%d: Reusing stream %d", __func__, mId, i);
182 newStreams[i] = reuseStream(astream);
183 } else {
184 ALOGV("%s:%d: Creating new stream %d", __func__, mId, i);
185 newStreams[i] = new Stream(mId, astream);
186 }
187
188 if (newStreams[i] == NULL) {
189 ALOGE("%s:%d: Error processing stream %d", __func__, mId, i);
190 goto err_out;
191 }
192 astream->priv = newStreams[i];
193 }
194
195 // Verify the set of streams in aggregate
196 if (!isValidStreamSet(newStreams, stream_config->num_streams)) {
197 ALOGE("%s:%d: Invalid stream set", __func__, mId);
198 goto err_out;
199 }
200
201 // Set up all streams (calculate usage/max_buffers for each)
202 setupStreams(newStreams, stream_config->num_streams);
203
204 // Destroy all old streams and replace stream array with new one
205 destroyStreams(mStreams, mNumStreams);
206 mStreams = newStreams;
207 mNumStreams = stream_config->num_streams;
208
209 // Clear out last seen settings metadata
210 setSettings(NULL);
211 return 0;
212
213err_out:
214 // Clean up temporary streams, preserve existing mStreams/mNumStreams
215 destroyStreams(newStreams, stream_config->num_streams);
216 return -EINVAL;
217}
218
219void Camera::destroyStreams(Stream **streams, int count)
220{
221 if (streams == NULL)
222 return;
223 for (int i = 0; i < count; i++) {
224 // Only destroy streams that weren't reused
225 if (streams[i] != NULL && !streams[i]->mReuse)
226 delete streams[i];
227 }
228 delete [] streams;
229}
230
231Stream *Camera::reuseStream(camera3_stream_t *astream)
232{
233 Stream *priv = reinterpret_cast<Stream*>(astream->priv);
234 // Verify the re-used stream's parameters match
235 if (!priv->isValidReuseStream(mId, astream)) {
236 ALOGE("%s:%d: Mismatched parameter in reused stream", __func__, mId);
237 return NULL;
238 }
239 // Mark stream to be reused
240 priv->mReuse = true;
241 return priv;
242}
243
244bool Camera::isValidStreamSet(Stream **streams, int count)
245{
246 int inputs = 0;
247 int outputs = 0;
248
249 if (streams == NULL) {
250 ALOGE("%s:%d: NULL stream configuration streams", __func__, mId);
251 return false;
252 }
253 if (count == 0) {
254 ALOGE("%s:%d: Zero count stream configuration streams", __func__, mId);
255 return false;
256 }
257 // Validate there is at most one input stream and at least one output stream
258 for (int i = 0; i < count; i++) {
259 // A stream may be both input and output (bidirectional)
260 if (streams[i]->isInputType())
261 inputs++;
262 if (streams[i]->isOutputType())
263 outputs++;
264 }
265 ALOGV("%s:%d: Configuring %d output streams and %d input streams",
266 __func__, mId, outputs, inputs);
267 if (outputs < 1) {
268 ALOGE("%s:%d: Stream config must have >= 1 output", __func__, mId);
269 return false;
270 }
271 if (inputs > 1) {
272 ALOGE("%s:%d: Stream config must have <= 1 input", __func__, mId);
273 return false;
274 }
275 // TODO: check for correct number of Bayer/YUV/JPEG/Encoder streams
276 return true;
277}
278
279void Camera::setupStreams(Stream **streams, int count)
280{
281 /*
282 * This is where the HAL has to decide internally how to handle all of the
283 * streams, and then produce usage and max_buffer values for each stream.
284 * Note, the stream array has been checked before this point for ALL invalid
285 * conditions, so it must find a successful configuration for this stream
286 * array. The HAL may not return an error from this point.
287 *
288 * In this demo HAL, we just set all streams to be the same dummy values;
289 * real implementations will want to avoid USAGE_SW_{READ|WRITE}_OFTEN.
290 */
291 for (int i = 0; i < count; i++) {
292 uint32_t usage = 0;
293
294 if (streams[i]->isOutputType())
295 usage |= GRALLOC_USAGE_SW_WRITE_OFTEN |
296 GRALLOC_USAGE_HW_CAMERA_WRITE;
297 if (streams[i]->isInputType())
298 usage |= GRALLOC_USAGE_SW_READ_OFTEN |
299 GRALLOC_USAGE_HW_CAMERA_READ;
300
301 streams[i]->setUsage(usage);
302 streams[i]->setMaxBuffers(1);
303 }
304}
305
306int Camera::registerStreamBuffers(const camera3_stream_buffer_set_t *buf_set)
307{
308 ALOGV("%s:%d: buffer_set=%p", __func__, mId, buf_set);
309 if (buf_set == NULL) {
310 ALOGE("%s:%d: NULL buffer set", __func__, mId);
311 return -EINVAL;
312 }
313 if (buf_set->stream == NULL) {
314 ALOGE("%s:%d: NULL stream handle", __func__, mId);
315 return -EINVAL;
316 }
317 Stream *stream = reinterpret_cast<Stream*>(buf_set->stream->priv);
318 return stream->registerBuffers(buf_set);
319}
320
321bool Camera::isValidTemplateType(int type)
322{
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700323 return type > 0 && type < CAMERA3_TEMPLATE_COUNT;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700324}
325
326const camera_metadata_t* Camera::constructDefaultRequestSettings(int type)
327{
328 ALOGV("%s:%d: type=%d", __func__, mId, type);
329
330 if (!isValidTemplateType(type)) {
331 ALOGE("%s:%d: Invalid template request type: %d", __func__, mId, type);
332 return NULL;
333 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700334
335 // Will return NULL (indicating unsupported) if the template is not
336 // initialized.
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700337 return mTemplates[type];
338}
339
340int Camera::processCaptureRequest(camera3_capture_request_t *request)
341{
342 camera3_capture_result result;
343
344 ALOGV("%s:%d: request=%p", __func__, mId, request);
345 ATRACE_CALL();
346
347 if (request == NULL) {
348 ALOGE("%s:%d: NULL request recieved", __func__, mId);
349 return -EINVAL;
350 }
351
352 ALOGV("%s:%d: Request Frame:%d Settings:%p", __func__, mId,
353 request->frame_number, request->settings);
354
355 // NULL indicates use last settings
356 if (request->settings == NULL) {
357 if (mSettings == NULL) {
358 ALOGE("%s:%d: NULL settings without previous set Frame:%d Req:%p",
359 __func__, mId, request->frame_number, request);
360 return -EINVAL;
361 }
362 } else {
363 setSettings(request->settings);
364 }
365
366 if (request->input_buffer != NULL) {
367 ALOGV("%s:%d: Reprocessing input buffer %p", __func__, mId,
368 request->input_buffer);
369
370 if (!isValidReprocessSettings(request->settings)) {
371 ALOGE("%s:%d: Invalid settings for reprocess request: %p",
372 __func__, mId, request->settings);
373 return -EINVAL;
374 }
375 } else {
376 ALOGV("%s:%d: Capturing new frame.", __func__, mId);
377
378 if (!isValidCaptureSettings(request->settings)) {
379 ALOGE("%s:%d: Invalid settings for capture request: %p",
380 __func__, mId, request->settings);
381 return -EINVAL;
382 }
383 }
384
385 if (request->num_output_buffers <= 0) {
386 ALOGE("%s:%d: Invalid number of output buffers: %d", __func__, mId,
387 request->num_output_buffers);
388 return -EINVAL;
389 }
390 result.num_output_buffers = request->num_output_buffers;
391 result.output_buffers = new camera3_stream_buffer_t[result.num_output_buffers];
392 for (unsigned int i = 0; i < request->num_output_buffers; i++) {
393 int res = processCaptureBuffer(&request->output_buffers[i],
394 const_cast<camera3_stream_buffer_t*>(&result.output_buffers[i]));
395 if (res)
396 goto err_out;
397 }
398
399 result.frame_number = request->frame_number;
400 // TODO: return actual captured/reprocessed settings
401 result.result = request->settings;
402 // TODO: asynchronously return results
403 notifyShutter(request->frame_number, 0);
404 mCallbackOps->process_capture_result(mCallbackOps, &result);
405
406 return 0;
407
408err_out:
409 delete [] result.output_buffers;
410 // TODO: this should probably be a total device failure; transient for now
411 return -EINVAL;
412}
413
414void Camera::setSettings(const camera_metadata_t *new_settings)
415{
416 if (mSettings != NULL) {
417 free_camera_metadata(mSettings);
418 mSettings = NULL;
419 }
420
421 if (new_settings != NULL)
422 mSettings = clone_camera_metadata(new_settings);
423}
424
425bool Camera::isValidReprocessSettings(const camera_metadata_t* /*settings*/)
426{
427 // TODO: reject settings that cannot be reprocessed
428 // input buffers unimplemented, use this to reject reprocessing requests
429 ALOGE("%s:%d: Input buffer reprocessing not implemented", __func__, mId);
430 return false;
431}
432
433int Camera::processCaptureBuffer(const camera3_stream_buffer_t *in,
434 camera3_stream_buffer_t *out)
435{
436 if (in->acquire_fence != -1) {
437 int res = sync_wait(in->acquire_fence, CAMERA_SYNC_TIMEOUT);
438 if (res == -ETIME) {
439 ALOGE("%s:%d: Timeout waiting on buffer acquire fence",
440 __func__, mId);
441 return res;
442 } else if (res) {
443 ALOGE("%s:%d: Error waiting on buffer acquire fence: %s(%d)",
444 __func__, mId, strerror(-res), res);
445 return res;
446 }
447 }
448
449 out->stream = in->stream;
450 out->buffer = in->buffer;
451 out->status = CAMERA3_BUFFER_STATUS_OK;
452 // TODO: use driver-backed release fences
453 out->acquire_fence = -1;
454 out->release_fence = -1;
455
456 // TODO: lock and software-paint buffer
457 return 0;
458}
459
460void Camera::notifyShutter(uint32_t frame_number, uint64_t timestamp)
461{
462 int res;
463 struct timespec ts;
464
465 // If timestamp is 0, get timestamp from right now instead
466 if (timestamp == 0) {
467 ALOGW("%s:%d: No timestamp provided, using CLOCK_BOOTTIME",
468 __func__, mId);
469 res = clock_gettime(CLOCK_BOOTTIME, &ts);
470 if (res == 0) {
471 timestamp = ts.tv_sec * 1000000000ULL + ts.tv_nsec;
472 } else {
473 ALOGE("%s:%d: No timestamp and failed to get CLOCK_BOOTTIME %s(%d)",
474 __func__, mId, strerror(errno), errno);
475 }
476 }
477 camera3_notify_msg_t m;
478 memset(&m, 0, sizeof(m));
479 m.type = CAMERA3_MSG_SHUTTER;
480 m.message.shutter.frame_number = frame_number;
481 m.message.shutter.timestamp = timestamp;
482 mCallbackOps->notify(mCallbackOps, &m);
483}
484
485void Camera::dump(int fd)
486{
487 ALOGV("%s:%d: Dumping to fd %d", __func__, mId, fd);
488 ATRACE_CALL();
489 android::Mutex::Autolock al(mDeviceLock);
490
491 dprintf(fd, "Camera ID: %d (Busy: %d)\n", mId, mBusy);
492
493 // TODO: dump all settings
494 dprintf(fd, "Most Recent Settings: (%p)\n", mSettings);
495
496 dprintf(fd, "Number of streams: %d\n", mNumStreams);
497 for (int i = 0; i < mNumStreams; i++) {
498 dprintf(fd, "Stream %d/%d:\n", i, mNumStreams);
499 mStreams[i]->dump(fd);
500 }
501}
502
503const char* Camera::templateToString(int type)
504{
505 switch (type) {
506 case CAMERA3_TEMPLATE_PREVIEW:
507 return "CAMERA3_TEMPLATE_PREVIEW";
508 case CAMERA3_TEMPLATE_STILL_CAPTURE:
509 return "CAMERA3_TEMPLATE_STILL_CAPTURE";
510 case CAMERA3_TEMPLATE_VIDEO_RECORD:
511 return "CAMERA3_TEMPLATE_VIDEO_RECORD";
512 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
513 return "CAMERA3_TEMPLATE_VIDEO_SNAPSHOT";
514 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG:
515 return "CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG";
516 }
517 // TODO: support vendor templates
518 return "Invalid template type!";
519}
520
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700521int Camera::setTemplate(int type, const camera_metadata_t *settings)
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700522{
523 android::Mutex::Autolock al(mDeviceLock);
524
525 if (!isValidTemplateType(type)) {
526 ALOGE("%s:%d: Invalid template request type: %d", __func__, mId, type);
527 return -EINVAL;
528 }
529
530 if (mTemplates[type] != NULL) {
531 ALOGE("%s:%d: Setting already constructed template type %s(%d)",
532 __func__, mId, templateToString(type), type);
533 return -EINVAL;
534 }
535
536 // Make a durable copy of the underlying metadata
537 mTemplates[type] = clone_camera_metadata(settings);
538 if (mTemplates[type] == NULL) {
539 ALOGE("%s:%d: Failed to clone metadata %p for template type %s(%d)",
540 __func__, mId, settings, templateToString(type), type);
541 return -EINVAL;
542 }
543 return 0;
544}
545
546extern "C" {
547// Get handle to camera from device priv data
548static Camera *camdev_to_camera(const camera3_device_t *dev)
549{
550 return reinterpret_cast<Camera*>(dev->priv);
551}
552
553static int initialize(const camera3_device_t *dev,
554 const camera3_callback_ops_t *callback_ops)
555{
556 return camdev_to_camera(dev)->initialize(callback_ops);
557}
558
559static int configure_streams(const camera3_device_t *dev,
560 camera3_stream_configuration_t *stream_list)
561{
562 return camdev_to_camera(dev)->configureStreams(stream_list);
563}
564
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700565// TODO(b/29221641): remove
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700566static int register_stream_buffers(const camera3_device_t *dev,
567 const camera3_stream_buffer_set_t *buffer_set)
568{
569 return camdev_to_camera(dev)->registerStreamBuffers(buffer_set);
570}
571
572static const camera_metadata_t *construct_default_request_settings(
573 const camera3_device_t *dev, int type)
574{
575 return camdev_to_camera(dev)->constructDefaultRequestSettings(type);
576}
577
578static int process_capture_request(const camera3_device_t *dev,
579 camera3_capture_request_t *request)
580{
581 return camdev_to_camera(dev)->processCaptureRequest(request);
582}
583
584static void dump(const camera3_device_t *dev, int fd)
585{
586 camdev_to_camera(dev)->dump(fd);
587}
588
589static int flush(const camera3_device_t*)
590{
591 ALOGE("%s: unimplemented.", __func__);
592 return -1;
593}
594
595} // extern "C"
596
597const camera3_device_ops_t Camera::sOps = {
598 .initialize = default_camera_hal::initialize,
599 .configure_streams = default_camera_hal::configure_streams,
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700600 .register_stream_buffers = nullptr,
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700601 .construct_default_request_settings
602 = default_camera_hal::construct_default_request_settings,
603 .process_capture_request = default_camera_hal::process_capture_request,
604 .get_metadata_vendor_tag_ops = NULL,
605 .dump = default_camera_hal::dump,
606 .flush = default_camera_hal::flush,
607 .reserved = {0},
608};
609
610} // namespace default_camera_hal