blob: 57cdb4702f46c71b2dc1b5e6b2393abbcdea3fe4 [file] [log] [blame]
Alex Ray7ee0b7a2012-11-06 00:12:49 -08001/*
2 * Copyright (C) 2012 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#include <cstdlib>
Alex Ray69f1f912013-10-21 00:46:44 -070018#include <stdio.h>
Alex Raya0ed4be2013-02-25 15:02:16 -080019#include <hardware/camera3.h>
Alex Ray083315c2013-04-26 19:32:29 -070020#include <sync/sync.h>
Alex Raybfcbd952013-03-20 13:20:02 -070021#include <system/camera_metadata.h>
Alex Rayb0be1032013-05-28 15:52:47 -070022#include <system/graphics.h>
Alex Ray55567642013-11-12 12:58:39 -080023#include <utils/Mutex.h>
Alex Raya0ed4be2013-02-25 15:02:16 -080024#include "CameraHAL.h"
Alex Rayb0be1032013-05-28 15:52:47 -070025#include "Metadata.h"
Alex Raybcaf7882013-02-28 16:04:35 -080026#include "Stream.h"
Alex Ray7ee0b7a2012-11-06 00:12:49 -080027
Alex Rayed6b8a72012-12-27 10:42:22 -080028//#define LOG_NDEBUG 0
Alex Ray7ee0b7a2012-11-06 00:12:49 -080029#define LOG_TAG "Camera"
30#include <cutils/log.h>
31
Alex Rayed6b8a72012-12-27 10:42:22 -080032#define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
Alex Rayea803822013-10-14 15:56:43 -070033#include <utils/Trace.h>
Alex Rayed6b8a72012-12-27 10:42:22 -080034
Alex Ray7ee0b7a2012-11-06 00:12:49 -080035#include "Camera.h"
36
Alex Ray083315c2013-04-26 19:32:29 -070037#define CAMERA_SYNC_TIMEOUT 5000 // in msecs
38
Alex Ray7ee0b7a2012-11-06 00:12:49 -080039namespace 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{
Alex Raya0ed4be2013-02-25 15:02:16 -080045 camera3_device_t* cam_dev = reinterpret_cast<camera3_device_t*>(dev);
Alex Ray7ee0b7a2012-11-06 00:12:49 -080046 Camera* cam = static_cast<Camera*>(cam_dev->priv);
47 return cam->close();
48}
49} // extern "C"
50
Alex Raya0ed4be2013-02-25 15:02:16 -080051Camera::Camera(int id)
52 : mId(id),
Alex Rayb0be1032013-05-28 15:52:47 -070053 mStaticInfo(NULL),
Alex Raya0ed4be2013-02-25 15:02:16 -080054 mBusy(false),
Alex Raybcaf7882013-02-28 16:04:35 -080055 mCallbackOps(NULL),
56 mStreams(NULL),
Alex Raybfcbd952013-03-20 13:20:02 -070057 mNumStreams(0),
58 mSettings(NULL)
Alex Ray7ee0b7a2012-11-06 00:12:49 -080059{
Alex Ray61f7a0c2013-07-03 17:54:19 -070060 memset(&mTemplates, 0, sizeof(mTemplates));
Alex Raya0ed4be2013-02-25 15:02:16 -080061 memset(&mDevice, 0, sizeof(mDevice));
Alex Ray7ee0b7a2012-11-06 00:12:49 -080062 mDevice.common.tag = HARDWARE_DEVICE_TAG;
Alex Rayb0be1032013-05-28 15:52:47 -070063 mDevice.common.version = CAMERA_DEVICE_API_VERSION_3_0;
Alex Ray7ee0b7a2012-11-06 00:12:49 -080064 mDevice.common.close = close_device;
Alex Raya0ed4be2013-02-25 15:02:16 -080065 mDevice.ops = const_cast<camera3_device_ops_t*>(&sOps);
Alex Ray7ee0b7a2012-11-06 00:12:49 -080066 mDevice.priv = this;
67}
68
69Camera::~Camera()
70{
Alex Ray61f7a0c2013-07-03 17:54:19 -070071 if (mStaticInfo != NULL) {
72 free_camera_metadata(mStaticInfo);
73 }
Alex Ray7ee0b7a2012-11-06 00:12:49 -080074}
75
Alex Raya0ed4be2013-02-25 15:02:16 -080076int Camera::open(const hw_module_t *module, hw_device_t **device)
Alex Ray7ee0b7a2012-11-06 00:12:49 -080077{
Alex Raya0ed4be2013-02-25 15:02:16 -080078 ALOGI("%s:%d: Opening camera device", __func__, mId);
Alex Rayea803822013-10-14 15:56:43 -070079 ATRACE_CALL();
Alex Ray55567642013-11-12 12:58:39 -080080 android::Mutex::Autolock al(mDeviceLock);
81
Alex Ray7ee0b7a2012-11-06 00:12:49 -080082 if (mBusy) {
Alex Raya0ed4be2013-02-25 15:02:16 -080083 ALOGE("%s:%d: Error! Camera device already opened", __func__, mId);
Alex Ray7ee0b7a2012-11-06 00:12:49 -080084 return -EBUSY;
85 }
86
87 // TODO: open camera dev nodes, etc
88 mBusy = true;
Alex Raya0ed4be2013-02-25 15:02:16 -080089 mDevice.common.module = const_cast<hw_module_t*>(module);
90 *device = &mDevice.common;
Alex Ray7ee0b7a2012-11-06 00:12:49 -080091 return 0;
92}
93
Alex Rayb0be1032013-05-28 15:52:47 -070094int Camera::getInfo(struct camera_info *info)
95{
Alex Ray55567642013-11-12 12:58:39 -080096 android::Mutex::Autolock al(mStaticInfoLock);
97
Alex Rayb0be1032013-05-28 15:52:47 -070098 info->facing = CAMERA_FACING_FRONT;
99 info->orientation = 0;
100 info->device_version = mDevice.common.version;
Alex Rayb0be1032013-05-28 15:52:47 -0700101 if (mStaticInfo == NULL) {
Alex Ray0f82f5a2013-07-17 14:23:04 -0700102 mStaticInfo = initStaticInfo();
Alex Rayb0be1032013-05-28 15:52:47 -0700103 }
Alex Rayb0be1032013-05-28 15:52:47 -0700104 info->static_camera_characteristics = mStaticInfo;
Alex Ray0f82f5a2013-07-17 14:23:04 -0700105 return 0;
Alex Rayb0be1032013-05-28 15:52:47 -0700106}
107
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800108int Camera::close()
109{
Alex Raya0ed4be2013-02-25 15:02:16 -0800110 ALOGI("%s:%d: Closing camera device", __func__, mId);
Alex Rayea803822013-10-14 15:56:43 -0700111 ATRACE_CALL();
Alex Ray55567642013-11-12 12:58:39 -0800112 android::Mutex::Autolock al(mDeviceLock);
113
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800114 if (!mBusy) {
Alex Raya0ed4be2013-02-25 15:02:16 -0800115 ALOGE("%s:%d: Error! Camera device not open", __func__, mId);
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800116 return -EINVAL;
117 }
118
119 // TODO: close camera dev nodes, etc
120 mBusy = false;
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800121 return 0;
122}
123
Alex Raya0ed4be2013-02-25 15:02:16 -0800124int Camera::initialize(const camera3_callback_ops_t *callback_ops)
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800125{
Alex Ray61f7a0c2013-07-03 17:54:19 -0700126 int res;
127
Alex Raya0ed4be2013-02-25 15:02:16 -0800128 ALOGV("%s:%d: callback_ops=%p", __func__, mId, callback_ops);
129 mCallbackOps = callback_ops;
Alex Ray61f7a0c2013-07-03 17:54:19 -0700130 // per-device specific initialization
131 res = initDevice();
132 if (res != 0) {
133 ALOGE("%s:%d: Failed to initialize device!", __func__, mId);
134 return res;
Alex Ray89a82662013-05-28 20:32:48 -0700135 }
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800136 return 0;
137}
138
Alex Raybcaf7882013-02-28 16:04:35 -0800139int Camera::configureStreams(camera3_stream_configuration_t *stream_config)
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800140{
Alex Raybcaf7882013-02-28 16:04:35 -0800141 camera3_stream_t *astream;
142 Stream **newStreams = NULL;
143
Alex Raybcaf7882013-02-28 16:04:35 -0800144 ALOGV("%s:%d: stream_config=%p", __func__, mId, stream_config);
Alex Ray55567642013-11-12 12:58:39 -0800145 ATRACE_CALL();
146 android::Mutex::Autolock al(mDeviceLock);
Alex Raybcaf7882013-02-28 16:04:35 -0800147
148 if (stream_config == NULL) {
149 ALOGE("%s:%d: NULL stream configuration array", __func__, mId);
150 return -EINVAL;
151 }
152 if (stream_config->num_streams == 0) {
153 ALOGE("%s:%d: Empty stream configuration array", __func__, mId);
154 return -EINVAL;
155 }
156
157 // Create new stream array
158 newStreams = new Stream*[stream_config->num_streams];
159 ALOGV("%s:%d: Number of Streams: %d", __func__, mId,
160 stream_config->num_streams);
161
Alex Raybcaf7882013-02-28 16:04:35 -0800162 // Mark all current streams unused for now
163 for (int i = 0; i < mNumStreams; i++)
164 mStreams[i]->mReuse = false;
165 // Fill new stream array with reused streams and new streams
Alex Rayc6bf2f22013-05-28 15:52:04 -0700166 for (unsigned int i = 0; i < stream_config->num_streams; i++) {
Alex Raybcaf7882013-02-28 16:04:35 -0800167 astream = stream_config->streams[i];
Alex Ray2b286da2013-05-29 15:08:29 -0700168 if (astream->max_buffers > 0) {
169 ALOGV("%s:%d: Reusing stream %d", __func__, mId, i);
Alex Raybcaf7882013-02-28 16:04:35 -0800170 newStreams[i] = reuseStream(astream);
Alex Ray2b286da2013-05-29 15:08:29 -0700171 } else {
172 ALOGV("%s:%d: Creating new stream %d", __func__, mId, i);
Alex Raybcaf7882013-02-28 16:04:35 -0800173 newStreams[i] = new Stream(mId, astream);
Alex Ray2b286da2013-05-29 15:08:29 -0700174 }
Alex Raybcaf7882013-02-28 16:04:35 -0800175
176 if (newStreams[i] == NULL) {
177 ALOGE("%s:%d: Error processing stream %d", __func__, mId, i);
178 goto err_out;
179 }
180 astream->priv = newStreams[i];
181 }
182
183 // Verify the set of streams in aggregate
184 if (!isValidStreamSet(newStreams, stream_config->num_streams)) {
185 ALOGE("%s:%d: Invalid stream set", __func__, mId);
186 goto err_out;
187 }
188
189 // Set up all streams (calculate usage/max_buffers for each)
190 setupStreams(newStreams, stream_config->num_streams);
191
192 // Destroy all old streams and replace stream array with new one
193 destroyStreams(mStreams, mNumStreams);
194 mStreams = newStreams;
195 mNumStreams = stream_config->num_streams;
196
Alex Raybfcbd952013-03-20 13:20:02 -0700197 // Clear out last seen settings metadata
198 setSettings(NULL);
Alex Raya0ed4be2013-02-25 15:02:16 -0800199 return 0;
Alex Raybcaf7882013-02-28 16:04:35 -0800200
201err_out:
202 // Clean up temporary streams, preserve existing mStreams/mNumStreams
203 destroyStreams(newStreams, stream_config->num_streams);
Alex Raybcaf7882013-02-28 16:04:35 -0800204 return -EINVAL;
205}
206
207void Camera::destroyStreams(Stream **streams, int count)
208{
209 if (streams == NULL)
210 return;
211 for (int i = 0; i < count; i++) {
212 // Only destroy streams that weren't reused
213 if (streams[i] != NULL && !streams[i]->mReuse)
214 delete streams[i];
215 }
216 delete [] streams;
217}
218
219Stream *Camera::reuseStream(camera3_stream_t *astream)
220{
221 Stream *priv = reinterpret_cast<Stream*>(astream->priv);
222 // Verify the re-used stream's parameters match
223 if (!priv->isValidReuseStream(mId, astream)) {
224 ALOGE("%s:%d: Mismatched parameter in reused stream", __func__, mId);
225 return NULL;
226 }
227 // Mark stream to be reused
228 priv->mReuse = true;
229 return priv;
230}
231
232bool Camera::isValidStreamSet(Stream **streams, int count)
233{
234 int inputs = 0;
235 int outputs = 0;
236
237 if (streams == NULL) {
238 ALOGE("%s:%d: NULL stream configuration streams", __func__, mId);
239 return false;
240 }
241 if (count == 0) {
242 ALOGE("%s:%d: Zero count stream configuration streams", __func__, mId);
243 return false;
244 }
245 // Validate there is at most one input stream and at least one output stream
246 for (int i = 0; i < count; i++) {
247 // A stream may be both input and output (bidirectional)
248 if (streams[i]->isInputType())
249 inputs++;
250 if (streams[i]->isOutputType())
251 outputs++;
252 }
Alex Ray768216e2013-07-02 16:56:14 -0700253 ALOGV("%s:%d: Configuring %d output streams and %d input streams",
254 __func__, mId, outputs, inputs);
Alex Raybcaf7882013-02-28 16:04:35 -0800255 if (outputs < 1) {
256 ALOGE("%s:%d: Stream config must have >= 1 output", __func__, mId);
257 return false;
258 }
259 if (inputs > 1) {
260 ALOGE("%s:%d: Stream config must have <= 1 input", __func__, mId);
261 return false;
262 }
263 // TODO: check for correct number of Bayer/YUV/JPEG/Encoder streams
264 return true;
265}
266
267void Camera::setupStreams(Stream **streams, int count)
268{
269 /*
270 * This is where the HAL has to decide internally how to handle all of the
271 * streams, and then produce usage and max_buffer values for each stream.
272 * Note, the stream array has been checked before this point for ALL invalid
273 * conditions, so it must find a successful configuration for this stream
274 * array. The HAL may not return an error from this point.
275 *
276 * In this demo HAL, we just set all streams to be the same dummy values;
277 * real implementations will want to avoid USAGE_SW_{READ|WRITE}_OFTEN.
278 */
279 for (int i = 0; i < count; i++) {
280 uint32_t usage = 0;
281
282 if (streams[i]->isOutputType())
283 usage |= GRALLOC_USAGE_SW_WRITE_OFTEN |
284 GRALLOC_USAGE_HW_CAMERA_WRITE;
285 if (streams[i]->isInputType())
286 usage |= GRALLOC_USAGE_SW_READ_OFTEN |
287 GRALLOC_USAGE_HW_CAMERA_READ;
288
289 streams[i]->setUsage(usage);
290 streams[i]->setMaxBuffers(1);
291 }
Alex Raya0ed4be2013-02-25 15:02:16 -0800292}
293
294int Camera::registerStreamBuffers(const camera3_stream_buffer_set_t *buf_set)
295{
296 ALOGV("%s:%d: buffer_set=%p", __func__, mId, buf_set);
Alex Ray8a8f86b2013-03-01 01:32:21 -0800297 if (buf_set == NULL) {
298 ALOGE("%s:%d: NULL buffer set", __func__, mId);
299 return -EINVAL;
300 }
301 if (buf_set->stream == NULL) {
302 ALOGE("%s:%d: NULL stream handle", __func__, mId);
303 return -EINVAL;
304 }
305 Stream *stream = reinterpret_cast<Stream*>(buf_set->stream->priv);
306 return stream->registerBuffers(buf_set);
Alex Raya0ed4be2013-02-25 15:02:16 -0800307}
308
Alex Ray61f7a0c2013-07-03 17:54:19 -0700309bool Camera::isValidTemplateType(int type)
310{
311 return type < 1 || type >= CAMERA3_TEMPLATE_COUNT;
312}
313
Alex Raya0ed4be2013-02-25 15:02:16 -0800314const camera_metadata_t* Camera::constructDefaultRequestSettings(int type)
315{
316 ALOGV("%s:%d: type=%d", __func__, mId, type);
Alex Ray89a82662013-05-28 20:32:48 -0700317
Alex Ray61f7a0c2013-07-03 17:54:19 -0700318 if (!isValidTemplateType(type)) {
Alex Ray89a82662013-05-28 20:32:48 -0700319 ALOGE("%s:%d: Invalid template request type: %d", __func__, mId, type);
320 return NULL;
321 }
Alex Ray61f7a0c2013-07-03 17:54:19 -0700322 return mTemplates[type];
Alex Raya0ed4be2013-02-25 15:02:16 -0800323}
324
325int Camera::processCaptureRequest(camera3_capture_request_t *request)
326{
Alex Ray083315c2013-04-26 19:32:29 -0700327 camera3_capture_result result;
328
Alex Raya0ed4be2013-02-25 15:02:16 -0800329 ALOGV("%s:%d: request=%p", __func__, mId, request);
Alex Rayea803822013-10-14 15:56:43 -0700330 ATRACE_CALL();
Alex Raya0ed4be2013-02-25 15:02:16 -0800331
332 if (request == NULL) {
333 ALOGE("%s:%d: NULL request recieved", __func__, mId);
Alex Raya0ed4be2013-02-25 15:02:16 -0800334 return -EINVAL;
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800335 }
336
Alex Raybfcbd952013-03-20 13:20:02 -0700337 ALOGV("%s:%d: Request Frame:%d Settings:%p", __func__, mId,
338 request->frame_number, request->settings);
339
340 // NULL indicates use last settings
341 if (request->settings == NULL) {
342 if (mSettings == NULL) {
343 ALOGE("%s:%d: NULL settings without previous set Frame:%d Req:%p",
344 __func__, mId, request->frame_number, request);
345 return -EINVAL;
346 }
347 } else {
348 setSettings(request->settings);
349 }
350
Alex Ray11bbeef2013-04-26 14:47:08 -0700351 if (request->input_buffer != NULL) {
352 ALOGV("%s:%d: Reprocessing input buffer %p", __func__, mId,
353 request->input_buffer);
354
355 if (!isValidReprocessSettings(request->settings)) {
356 ALOGE("%s:%d: Invalid settings for reprocess request: %p",
357 __func__, mId, request->settings);
358 return -EINVAL;
359 }
360 } else {
361 ALOGV("%s:%d: Capturing new frame.", __func__, mId);
362
363 if (!isValidCaptureSettings(request->settings)) {
364 ALOGE("%s:%d: Invalid settings for capture request: %p",
365 __func__, mId, request->settings);
366 return -EINVAL;
367 }
368 }
369
Alex Ray083315c2013-04-26 19:32:29 -0700370 if (request->num_output_buffers <= 0) {
371 ALOGE("%s:%d: Invalid number of output buffers: %d", __func__, mId,
372 request->num_output_buffers);
373 return -EINVAL;
374 }
375 result.num_output_buffers = request->num_output_buffers;
376 result.output_buffers = new camera3_stream_buffer_t[result.num_output_buffers];
377 for (unsigned int i = 0; i < request->num_output_buffers; i++) {
378 int res = processCaptureBuffer(&request->output_buffers[i],
379 const_cast<camera3_stream_buffer_t*>(&result.output_buffers[i]));
380 if (res)
381 goto err_out;
382 }
383
384 result.frame_number = request->frame_number;
385 // TODO: return actual captured/reprocessed settings
386 result.result = request->settings;
387 // TODO: asynchronously return results
Alex Ray764e4422013-06-04 12:38:07 -0700388 notifyShutter(request->frame_number, 0);
Alex Ray083315c2013-04-26 19:32:29 -0700389 mCallbackOps->process_capture_result(mCallbackOps, &result);
390
Alex Raya0ed4be2013-02-25 15:02:16 -0800391 return 0;
Alex Ray083315c2013-04-26 19:32:29 -0700392
393err_out:
394 delete [] result.output_buffers;
395 // TODO: this should probably be a total device failure; transient for now
396 return -EINVAL;
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800397}
398
Alex Raybfcbd952013-03-20 13:20:02 -0700399void Camera::setSettings(const camera_metadata_t *new_settings)
400{
401 if (mSettings != NULL) {
402 free_camera_metadata(mSettings);
403 mSettings = NULL;
404 }
405
406 if (new_settings != NULL)
407 mSettings = clone_camera_metadata(new_settings);
408}
409
Alex Rayc6bf2f22013-05-28 15:52:04 -0700410bool Camera::isValidReprocessSettings(const camera_metadata_t* /*settings*/)
Alex Ray11bbeef2013-04-26 14:47:08 -0700411{
412 // TODO: reject settings that cannot be reprocessed
413 // input buffers unimplemented, use this to reject reprocessing requests
414 ALOGE("%s:%d: Input buffer reprocessing not implemented", __func__, mId);
415 return false;
416}
417
Alex Ray083315c2013-04-26 19:32:29 -0700418int Camera::processCaptureBuffer(const camera3_stream_buffer_t *in,
419 camera3_stream_buffer_t *out)
420{
Alex Ray77ecfd72013-05-30 00:19:04 -0700421 if (in->acquire_fence != -1) {
422 int res = sync_wait(in->acquire_fence, CAMERA_SYNC_TIMEOUT);
423 if (res == -ETIME) {
424 ALOGE("%s:%d: Timeout waiting on buffer acquire fence",
425 __func__, mId);
426 return res;
427 } else if (res) {
428 ALOGE("%s:%d: Error waiting on buffer acquire fence: %s(%d)",
429 __func__, mId, strerror(-res), res);
430 return res;
431 }
Alex Ray083315c2013-04-26 19:32:29 -0700432 }
433
434 out->stream = in->stream;
435 out->buffer = in->buffer;
436 out->status = CAMERA3_BUFFER_STATUS_OK;
437 // TODO: use driver-backed release fences
438 out->acquire_fence = -1;
439 out->release_fence = -1;
440
441 // TODO: lock and software-paint buffer
442 return 0;
443}
444
Alex Ray764e4422013-06-04 12:38:07 -0700445void Camera::notifyShutter(uint32_t frame_number, uint64_t timestamp)
446{
447 int res;
448 struct timespec ts;
449
450 // If timestamp is 0, get timestamp from right now instead
451 if (timestamp == 0) {
452 ALOGW("%s:%d: No timestamp provided, using CLOCK_BOOTTIME",
453 __func__, mId);
454 res = clock_gettime(CLOCK_BOOTTIME, &ts);
455 if (res == 0) {
456 timestamp = ts.tv_sec * 1000000000ULL + ts.tv_nsec;
457 } else {
458 ALOGE("%s:%d: No timestamp and failed to get CLOCK_BOOTTIME %s(%d)",
459 __func__, mId, strerror(errno), errno);
460 }
461 }
462 camera3_notify_msg_t m;
463 memset(&m, 0, sizeof(m));
464 m.type = CAMERA3_MSG_SHUTTER;
465 m.message.shutter.frame_number = frame_number;
466 m.message.shutter.timestamp = timestamp;
467 mCallbackOps->notify(mCallbackOps, &m);
468}
469
Alex Raya0ed4be2013-02-25 15:02:16 -0800470void Camera::getMetadataVendorTagOps(vendor_tag_query_ops_t *ops)
471{
472 ALOGV("%s:%d: ops=%p", __func__, mId, ops);
473 // TODO: return vendor tag ops
474}
475
476void Camera::dump(int fd)
477{
Alex Rayaf3a4612013-04-29 14:16:10 -0700478 ALOGV("%s:%d: Dumping to fd %d", __func__, mId, fd);
Alex Ray55567642013-11-12 12:58:39 -0800479 ATRACE_CALL();
480 android::Mutex::Autolock al(mDeviceLock);
Alex Ray69f1f912013-10-21 00:46:44 -0700481
482 fdprintf(fd, "Camera ID: %d (Busy: %d)\n", mId, mBusy);
483
484 // TODO: dump all settings
485 fdprintf(fd, "Most Recent Settings: (%p)\n", mSettings);
486
487 fdprintf(fd, "Number of streams: %d\n", mNumStreams);
488 for (int i = 0; i < mNumStreams; i++) {
489 fdprintf(fd, "Stream %d/%d:\n", i, mNumStreams);
490 mStreams[i]->dump(fd);
491 }
Alex Raya0ed4be2013-02-25 15:02:16 -0800492}
493
Alex Ray62735082013-10-21 12:55:24 -0700494const char* Camera::templateToString(int type)
495{
496 switch (type) {
497 case CAMERA3_TEMPLATE_PREVIEW:
498 return "CAMERA3_TEMPLATE_PREVIEW";
499 case CAMERA3_TEMPLATE_STILL_CAPTURE:
500 return "CAMERA3_TEMPLATE_STILL_CAPTURE";
501 case CAMERA3_TEMPLATE_VIDEO_RECORD:
502 return "CAMERA3_TEMPLATE_VIDEO_RECORD";
503 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
504 return "CAMERA3_TEMPLATE_VIDEO_SNAPSHOT";
505 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG:
506 return "CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG";
507 }
508 // TODO: support vendor templates
509 return "Invalid template type!";
510}
511
512int Camera::setTemplate(int type, camera_metadata_t *settings)
Alex Ray61f7a0c2013-07-03 17:54:19 -0700513{
Alex Ray55567642013-11-12 12:58:39 -0800514 android::Mutex::Autolock al(mDeviceLock);
515
Alex Ray61f7a0c2013-07-03 17:54:19 -0700516 if (!isValidTemplateType(type)) {
517 ALOGE("%s:%d: Invalid template request type: %d", __func__, mId, type);
Alex Ray62735082013-10-21 12:55:24 -0700518 return -EINVAL;
Alex Ray61f7a0c2013-07-03 17:54:19 -0700519 }
Alex Ray55567642013-11-12 12:58:39 -0800520
Alex Ray61f7a0c2013-07-03 17:54:19 -0700521 if (mTemplates[type] != NULL) {
Alex Ray62735082013-10-21 12:55:24 -0700522 ALOGE("%s:%d: Setting already constructed template type %s(%d)",
523 __func__, mId, templateToString(type), type);
Alex Ray62735082013-10-21 12:55:24 -0700524 return -EINVAL;
Alex Ray61f7a0c2013-07-03 17:54:19 -0700525 }
Alex Ray55567642013-11-12 12:58:39 -0800526
Alex Ray62735082013-10-21 12:55:24 -0700527 // Make a durable copy of the underlying metadata
Alex Ray61f7a0c2013-07-03 17:54:19 -0700528 mTemplates[type] = clone_camera_metadata(settings);
Alex Ray62735082013-10-21 12:55:24 -0700529 if (mTemplates[type] == NULL) {
530 ALOGE("%s:%d: Failed to clone metadata %p for template type %s(%d)",
531 __func__, mId, settings, templateToString(type), type);
Alex Ray62735082013-10-21 12:55:24 -0700532 return -EINVAL;
533 }
Alex Ray62735082013-10-21 12:55:24 -0700534 return 0;
Alex Ray61f7a0c2013-07-03 17:54:19 -0700535}
536
Alex Raya0ed4be2013-02-25 15:02:16 -0800537extern "C" {
538// Get handle to camera from device priv data
539static Camera *camdev_to_camera(const camera3_device_t *dev)
540{
541 return reinterpret_cast<Camera*>(dev->priv);
542}
543
544static int initialize(const camera3_device_t *dev,
545 const camera3_callback_ops_t *callback_ops)
546{
547 return camdev_to_camera(dev)->initialize(callback_ops);
548}
549
550static int configure_streams(const camera3_device_t *dev,
551 camera3_stream_configuration_t *stream_list)
552{
553 return camdev_to_camera(dev)->configureStreams(stream_list);
554}
555
556static int register_stream_buffers(const camera3_device_t *dev,
557 const camera3_stream_buffer_set_t *buffer_set)
558{
559 return camdev_to_camera(dev)->registerStreamBuffers(buffer_set);
560}
561
562static const camera_metadata_t *construct_default_request_settings(
563 const camera3_device_t *dev, int type)
564{
565 return camdev_to_camera(dev)->constructDefaultRequestSettings(type);
566}
567
568static int process_capture_request(const camera3_device_t *dev,
569 camera3_capture_request_t *request)
570{
571 return camdev_to_camera(dev)->processCaptureRequest(request);
572}
573
574static void get_metadata_vendor_tag_ops(const camera3_device_t *dev,
575 vendor_tag_query_ops_t *ops)
576{
577 camdev_to_camera(dev)->getMetadataVendorTagOps(ops);
578}
579
580static void dump(const camera3_device_t *dev, int fd)
581{
582 camdev_to_camera(dev)->dump(fd);
583}
584} // extern "C"
585
586const camera3_device_ops_t Camera::sOps = {
587 .initialize = default_camera_hal::initialize,
588 .configure_streams = default_camera_hal::configure_streams,
589 .register_stream_buffers = default_camera_hal::register_stream_buffers,
590 .construct_default_request_settings =
591 default_camera_hal::construct_default_request_settings,
592 .process_capture_request = default_camera_hal::process_capture_request,
593 .get_metadata_vendor_tag_ops =
594 default_camera_hal::get_metadata_vendor_tag_ops,
595 .dump = default_camera_hal::dump
596};
597
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800598} // namespace default_camera_hal