blob: 0a0b4934d4461ba9cce30077e1da230302295ba9 [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>
18#include <pthread.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 Raya0ed4be2013-02-25 15:02:16 -080023#include "CameraHAL.h"
Alex Rayb0be1032013-05-28 15:52:47 -070024#include "Metadata.h"
Alex Raybcaf7882013-02-28 16:04:35 -080025#include "Stream.h"
Alex Ray7ee0b7a2012-11-06 00:12:49 -080026
Alex Rayed6b8a72012-12-27 10:42:22 -080027//#define LOG_NDEBUG 0
Alex Ray7ee0b7a2012-11-06 00:12:49 -080028#define LOG_TAG "Camera"
29#include <cutils/log.h>
30
Alex Rayed6b8a72012-12-27 10:42:22 -080031#define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
32#include <cutils/trace.h>
Alex Rayc16e56d2013-04-29 12:24:49 -070033#include "ScopedTrace.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 Rayb0be1032013-05-28 15:52:47 -070039#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
40
Alex Ray7ee0b7a2012-11-06 00:12:49 -080041namespace default_camera_hal {
42
43extern "C" {
44// Shim passed to the framework to close an opened device.
45static int close_device(hw_device_t* dev)
46{
Alex Raya0ed4be2013-02-25 15:02:16 -080047 camera3_device_t* cam_dev = reinterpret_cast<camera3_device_t*>(dev);
Alex Ray7ee0b7a2012-11-06 00:12:49 -080048 Camera* cam = static_cast<Camera*>(cam_dev->priv);
49 return cam->close();
50}
51} // extern "C"
52
Alex Raya0ed4be2013-02-25 15:02:16 -080053Camera::Camera(int id)
54 : mId(id),
Alex Rayb0be1032013-05-28 15:52:47 -070055 mStaticInfo(NULL),
Alex Raya0ed4be2013-02-25 15:02:16 -080056 mBusy(false),
Alex Raybcaf7882013-02-28 16:04:35 -080057 mCallbackOps(NULL),
58 mStreams(NULL),
Alex Raybfcbd952013-03-20 13:20:02 -070059 mNumStreams(0),
60 mSettings(NULL)
Alex Ray7ee0b7a2012-11-06 00:12:49 -080061{
62 pthread_mutex_init(&mMutex,
63 NULL); // No pthread mutex attributes.
64
Alex Raya0ed4be2013-02-25 15:02:16 -080065 memset(&mDevice, 0, sizeof(mDevice));
Alex Ray7ee0b7a2012-11-06 00:12:49 -080066 mDevice.common.tag = HARDWARE_DEVICE_TAG;
Alex Rayb0be1032013-05-28 15:52:47 -070067 mDevice.common.version = CAMERA_DEVICE_API_VERSION_3_0;
Alex Ray7ee0b7a2012-11-06 00:12:49 -080068 mDevice.common.close = close_device;
Alex Raya0ed4be2013-02-25 15:02:16 -080069 mDevice.ops = const_cast<camera3_device_ops_t*>(&sOps);
Alex Ray7ee0b7a2012-11-06 00:12:49 -080070 mDevice.priv = this;
71}
72
73Camera::~Camera()
74{
75}
76
Alex Raya0ed4be2013-02-25 15:02:16 -080077int Camera::open(const hw_module_t *module, hw_device_t **device)
Alex Ray7ee0b7a2012-11-06 00:12:49 -080078{
Alex Raya0ed4be2013-02-25 15:02:16 -080079 ALOGI("%s:%d: Opening camera device", __func__, mId);
Alex Rayc16e56d2013-04-29 12:24:49 -070080 CAMTRACE_CALL();
Alex Ray7ee0b7a2012-11-06 00:12:49 -080081 pthread_mutex_lock(&mMutex);
82 if (mBusy) {
83 pthread_mutex_unlock(&mMutex);
Alex Raya0ed4be2013-02-25 15:02:16 -080084 ALOGE("%s:%d: Error! Camera device already opened", __func__, mId);
Alex Ray7ee0b7a2012-11-06 00:12:49 -080085 return -EBUSY;
86 }
87
88 // TODO: open camera dev nodes, etc
89 mBusy = true;
Alex Raya0ed4be2013-02-25 15:02:16 -080090 mDevice.common.module = const_cast<hw_module_t*>(module);
91 *device = &mDevice.common;
Alex Ray7ee0b7a2012-11-06 00:12:49 -080092
93 pthread_mutex_unlock(&mMutex);
94 return 0;
95}
96
Alex Rayb0be1032013-05-28 15:52:47 -070097int Camera::getInfo(struct camera_info *info)
98{
99 int ret = 0;
100
101 info->facing = CAMERA_FACING_FRONT;
102 info->orientation = 0;
103 info->device_version = mDevice.common.version;
104
105 pthread_mutex_lock(&mMutex);
106 if (mStaticInfo == NULL) {
107 ret = initStaticInfo();
108 }
109 pthread_mutex_unlock(&mMutex);
110
111 info->static_camera_characteristics = mStaticInfo;
112
113 return ret;
114}
115
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800116int Camera::close()
117{
Alex Raya0ed4be2013-02-25 15:02:16 -0800118 ALOGI("%s:%d: Closing camera device", __func__, mId);
Alex Rayc16e56d2013-04-29 12:24:49 -0700119 CAMTRACE_CALL();
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800120 pthread_mutex_lock(&mMutex);
121 if (!mBusy) {
122 pthread_mutex_unlock(&mMutex);
Alex Raya0ed4be2013-02-25 15:02:16 -0800123 ALOGE("%s:%d: Error! Camera device not open", __func__, mId);
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800124 return -EINVAL;
125 }
126
127 // TODO: close camera dev nodes, etc
128 mBusy = false;
129
130 pthread_mutex_unlock(&mMutex);
131 return 0;
132}
133
Alex Raya0ed4be2013-02-25 15:02:16 -0800134int Camera::initialize(const camera3_callback_ops_t *callback_ops)
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800135{
Alex Raya0ed4be2013-02-25 15:02:16 -0800136 ALOGV("%s:%d: callback_ops=%p", __func__, mId, callback_ops);
137 mCallbackOps = callback_ops;
Alex Ray89a82662013-05-28 20:32:48 -0700138 // Create standard settings templates
139 // 0 is invalid as template
140 mTemplates[0] = NULL;
141 // CAMERA3_TEMPLATE_PREVIEW = 1
142 mTemplates[1] = new Metadata(ANDROID_CONTROL_MODE_OFF,
143 ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW);
144 // CAMERA3_TEMPLATE_STILL_CAPTURE = 2
145 mTemplates[2] = new Metadata(ANDROID_CONTROL_MODE_OFF,
146 ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE);
147 // CAMERA3_TEMPLATE_VIDEO_RECORD = 3
148 mTemplates[3] = new Metadata(ANDROID_CONTROL_MODE_OFF,
149 ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD);
150 // CAMERA3_TEMPLATE_VIDEO_SNAPSHOT = 4
151 mTemplates[4] = new Metadata(ANDROID_CONTROL_MODE_OFF,
152 ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT);
153 // CAMERA3_TEMPLATE_STILL_ZERO_SHUTTER_LAG = 5
154 mTemplates[5] = new Metadata(ANDROID_CONTROL_MODE_OFF,
155 ANDROID_CONTROL_CAPTURE_INTENT_ZERO_SHUTTER_LAG);
156 // Pre-generate metadata structures
157 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
158 mTemplates[i]->generate();
159 }
160 // TODO: create vendor templates
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800161 return 0;
162}
163
Alex Rayb0be1032013-05-28 15:52:47 -0700164int Camera::initStaticInfo()
165{
166 /*
167 * Setup static camera info. This will have to customized per camera
168 * device.
169 */
170
171 /* android.control */
172 int32_t android_control_ae_available_target_fps_ranges[] = {30, 30};
173 mMetadata.addInt32(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES,
174 ARRAY_SIZE(android_control_ae_available_target_fps_ranges),
175 android_control_ae_available_target_fps_ranges);
176
177 int32_t android_control_ae_compensation_range[] = {-4, 4};
178 mMetadata.addInt32(ANDROID_CONTROL_AE_COMPENSATION_RANGE,
179 ARRAY_SIZE(android_control_ae_compensation_range),
180 android_control_ae_compensation_range);
181
182 camera_metadata_rational_t android_control_ae_compensation_step[] = {{2,1}};
183 mMetadata.addRational(ANDROID_CONTROL_AE_COMPENSATION_STEP,
184 ARRAY_SIZE(android_control_ae_compensation_step),
185 android_control_ae_compensation_step);
186
187 int32_t android_control_max_regions[] = {1};
188 mMetadata.addInt32(ANDROID_CONTROL_MAX_REGIONS,
189 ARRAY_SIZE(android_control_max_regions),
190 android_control_max_regions);
191
192 /* android.jpeg */
193 int32_t android_jpeg_available_thumbnail_sizes[] = {0, 0, 128, 96};
194 mMetadata.addInt32(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES,
195 ARRAY_SIZE(android_jpeg_available_thumbnail_sizes),
196 android_jpeg_available_thumbnail_sizes);
197
198 /* android.lens */
199 float android_lens_info_available_focal_lengths[] = {1.0};
200 mMetadata.addFloat(ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS,
201 ARRAY_SIZE(android_lens_info_available_focal_lengths),
202 android_lens_info_available_focal_lengths);
203
204 /* android.request */
205 int32_t android_request_max_num_output_streams[] = {0, 3, 1};
206 mMetadata.addInt32(ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS,
207 ARRAY_SIZE(android_request_max_num_output_streams),
208 android_request_max_num_output_streams);
209
210 /* android.scaler */
211 int32_t android_scaler_available_formats[] = {
212 HAL_PIXEL_FORMAT_RAW_SENSOR,
213 HAL_PIXEL_FORMAT_BLOB,
214 HAL_PIXEL_FORMAT_RGBA_8888,
215 HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED,
216 // These are handled by YCbCr_420_888
217 // HAL_PIXEL_FORMAT_YV12,
218 // HAL_PIXEL_FORMAT_YCrCb_420_SP,
219 HAL_PIXEL_FORMAT_YCbCr_420_888};
220 mMetadata.addInt32(ANDROID_SCALER_AVAILABLE_FORMATS,
221 ARRAY_SIZE(android_scaler_available_formats),
222 android_scaler_available_formats);
223
224 int64_t android_scaler_available_jpeg_min_durations[] = {1};
225 mMetadata.addInt64(ANDROID_SCALER_AVAILABLE_JPEG_MIN_DURATIONS,
226 ARRAY_SIZE(android_scaler_available_jpeg_min_durations),
227 android_scaler_available_jpeg_min_durations);
228
229 int32_t android_scaler_available_jpeg_sizes[] = {640, 480};
230 mMetadata.addInt32(ANDROID_SCALER_AVAILABLE_JPEG_SIZES,
231 ARRAY_SIZE(android_scaler_available_jpeg_sizes),
232 android_scaler_available_jpeg_sizes);
233
234 float android_scaler_available_max_digital_zoom[] = {1};
235 mMetadata.addFloat(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
236 ARRAY_SIZE(android_scaler_available_max_digital_zoom),
237 android_scaler_available_max_digital_zoom);
238
239 int64_t android_scaler_available_processed_min_durations[] = {1};
240 mMetadata.addInt64(ANDROID_SCALER_AVAILABLE_PROCESSED_MIN_DURATIONS,
241 ARRAY_SIZE(android_scaler_available_processed_min_durations),
242 android_scaler_available_processed_min_durations);
243
244 int32_t android_scaler_available_processed_sizes[] = {640, 480};
245 mMetadata.addInt32(ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES,
246 ARRAY_SIZE(android_scaler_available_processed_sizes),
247 android_scaler_available_processed_sizes);
248
249 int64_t android_scaler_available_raw_min_durations[] = {1};
250 mMetadata.addInt64(ANDROID_SCALER_AVAILABLE_RAW_MIN_DURATIONS,
251 ARRAY_SIZE(android_scaler_available_raw_min_durations),
252 android_scaler_available_raw_min_durations);
253
254 int32_t android_scaler_available_raw_sizes[] = {640, 480};
255 mMetadata.addInt32(ANDROID_SCALER_AVAILABLE_RAW_SIZES,
256 ARRAY_SIZE(android_scaler_available_raw_sizes),
257 android_scaler_available_raw_sizes);
258
259 /* android.sensor */
260
261 int32_t android_sensor_info_active_array_size[] = {0, 0, 640, 480};
262 mMetadata.addInt32(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE,
263 ARRAY_SIZE(android_sensor_info_active_array_size),
264 android_sensor_info_active_array_size);
265
266 int32_t android_sensor_info_available_sensitivities[] =
267 {100, 200, 400, 800, 1600};
268 mMetadata.addInt32(ANDROID_SENSOR_INFO_AVAILABLE_SENSITIVITIES,
269 ARRAY_SIZE(android_sensor_info_available_sensitivities),
270 android_sensor_info_available_sensitivities);
271
272 int64_t android_sensor_info_max_frame_duration[] = {30000000000};
273 mMetadata.addInt64(ANDROID_SENSOR_INFO_MAX_FRAME_DURATION,
274 ARRAY_SIZE(android_sensor_info_max_frame_duration),
275 android_sensor_info_max_frame_duration);
276
277 float android_sensor_info_physical_size[] = {3.2, 2.4};
278 mMetadata.addFloat(ANDROID_SENSOR_INFO_PHYSICAL_SIZE,
279 ARRAY_SIZE(android_sensor_info_physical_size),
280 android_sensor_info_physical_size);
281
282 int32_t android_sensor_info_pixel_array_size[] = {640, 480};
283 mMetadata.addInt32(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE,
284 ARRAY_SIZE(android_sensor_info_pixel_array_size),
285 android_sensor_info_pixel_array_size);
286
287 int32_t android_sensor_orientation[] = {0};
288 mMetadata.addInt32(ANDROID_SENSOR_ORIENTATION,
289 ARRAY_SIZE(android_sensor_orientation),
290 android_sensor_orientation);
291
292 /* End of static camera characteristics */
293
294 mStaticInfo = mMetadata.generate();
295
296 return 0;
297}
298
Alex Raybcaf7882013-02-28 16:04:35 -0800299int Camera::configureStreams(camera3_stream_configuration_t *stream_config)
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800300{
Alex Raybcaf7882013-02-28 16:04:35 -0800301 camera3_stream_t *astream;
302 Stream **newStreams = NULL;
303
304 CAMTRACE_CALL();
305 ALOGV("%s:%d: stream_config=%p", __func__, mId, stream_config);
306
307 if (stream_config == NULL) {
308 ALOGE("%s:%d: NULL stream configuration array", __func__, mId);
309 return -EINVAL;
310 }
311 if (stream_config->num_streams == 0) {
312 ALOGE("%s:%d: Empty stream configuration array", __func__, mId);
313 return -EINVAL;
314 }
315
316 // Create new stream array
317 newStreams = new Stream*[stream_config->num_streams];
318 ALOGV("%s:%d: Number of Streams: %d", __func__, mId,
319 stream_config->num_streams);
320
321 pthread_mutex_lock(&mMutex);
322
323 // Mark all current streams unused for now
324 for (int i = 0; i < mNumStreams; i++)
325 mStreams[i]->mReuse = false;
326 // Fill new stream array with reused streams and new streams
Alex Rayc6bf2f22013-05-28 15:52:04 -0700327 for (unsigned int i = 0; i < stream_config->num_streams; i++) {
Alex Raybcaf7882013-02-28 16:04:35 -0800328 astream = stream_config->streams[i];
329 if (astream->max_buffers > 0)
330 newStreams[i] = reuseStream(astream);
331 else
332 newStreams[i] = new Stream(mId, astream);
333
334 if (newStreams[i] == NULL) {
335 ALOGE("%s:%d: Error processing stream %d", __func__, mId, i);
336 goto err_out;
337 }
338 astream->priv = newStreams[i];
339 }
340
341 // Verify the set of streams in aggregate
342 if (!isValidStreamSet(newStreams, stream_config->num_streams)) {
343 ALOGE("%s:%d: Invalid stream set", __func__, mId);
344 goto err_out;
345 }
346
347 // Set up all streams (calculate usage/max_buffers for each)
348 setupStreams(newStreams, stream_config->num_streams);
349
350 // Destroy all old streams and replace stream array with new one
351 destroyStreams(mStreams, mNumStreams);
352 mStreams = newStreams;
353 mNumStreams = stream_config->num_streams;
354
Alex Raybfcbd952013-03-20 13:20:02 -0700355 // Clear out last seen settings metadata
356 setSettings(NULL);
357
Alex Raybcaf7882013-02-28 16:04:35 -0800358 pthread_mutex_unlock(&mMutex);
Alex Raya0ed4be2013-02-25 15:02:16 -0800359 return 0;
Alex Raybcaf7882013-02-28 16:04:35 -0800360
361err_out:
362 // Clean up temporary streams, preserve existing mStreams/mNumStreams
363 destroyStreams(newStreams, stream_config->num_streams);
364 pthread_mutex_unlock(&mMutex);
365 return -EINVAL;
366}
367
368void Camera::destroyStreams(Stream **streams, int count)
369{
370 if (streams == NULL)
371 return;
372 for (int i = 0; i < count; i++) {
373 // Only destroy streams that weren't reused
374 if (streams[i] != NULL && !streams[i]->mReuse)
375 delete streams[i];
376 }
377 delete [] streams;
378}
379
380Stream *Camera::reuseStream(camera3_stream_t *astream)
381{
382 Stream *priv = reinterpret_cast<Stream*>(astream->priv);
383 // Verify the re-used stream's parameters match
384 if (!priv->isValidReuseStream(mId, astream)) {
385 ALOGE("%s:%d: Mismatched parameter in reused stream", __func__, mId);
386 return NULL;
387 }
388 // Mark stream to be reused
389 priv->mReuse = true;
390 return priv;
391}
392
393bool Camera::isValidStreamSet(Stream **streams, int count)
394{
395 int inputs = 0;
396 int outputs = 0;
397
398 if (streams == NULL) {
399 ALOGE("%s:%d: NULL stream configuration streams", __func__, mId);
400 return false;
401 }
402 if (count == 0) {
403 ALOGE("%s:%d: Zero count stream configuration streams", __func__, mId);
404 return false;
405 }
406 // Validate there is at most one input stream and at least one output stream
407 for (int i = 0; i < count; i++) {
408 // A stream may be both input and output (bidirectional)
409 if (streams[i]->isInputType())
410 inputs++;
411 if (streams[i]->isOutputType())
412 outputs++;
413 }
414 if (outputs < 1) {
415 ALOGE("%s:%d: Stream config must have >= 1 output", __func__, mId);
416 return false;
417 }
418 if (inputs > 1) {
419 ALOGE("%s:%d: Stream config must have <= 1 input", __func__, mId);
420 return false;
421 }
422 // TODO: check for correct number of Bayer/YUV/JPEG/Encoder streams
423 return true;
424}
425
426void Camera::setupStreams(Stream **streams, int count)
427{
428 /*
429 * This is where the HAL has to decide internally how to handle all of the
430 * streams, and then produce usage and max_buffer values for each stream.
431 * Note, the stream array has been checked before this point for ALL invalid
432 * conditions, so it must find a successful configuration for this stream
433 * array. The HAL may not return an error from this point.
434 *
435 * In this demo HAL, we just set all streams to be the same dummy values;
436 * real implementations will want to avoid USAGE_SW_{READ|WRITE}_OFTEN.
437 */
438 for (int i = 0; i < count; i++) {
439 uint32_t usage = 0;
440
441 if (streams[i]->isOutputType())
442 usage |= GRALLOC_USAGE_SW_WRITE_OFTEN |
443 GRALLOC_USAGE_HW_CAMERA_WRITE;
444 if (streams[i]->isInputType())
445 usage |= GRALLOC_USAGE_SW_READ_OFTEN |
446 GRALLOC_USAGE_HW_CAMERA_READ;
447
448 streams[i]->setUsage(usage);
449 streams[i]->setMaxBuffers(1);
450 }
Alex Raya0ed4be2013-02-25 15:02:16 -0800451}
452
453int Camera::registerStreamBuffers(const camera3_stream_buffer_set_t *buf_set)
454{
455 ALOGV("%s:%d: buffer_set=%p", __func__, mId, buf_set);
Alex Ray8a8f86b2013-03-01 01:32:21 -0800456 if (buf_set == NULL) {
457 ALOGE("%s:%d: NULL buffer set", __func__, mId);
458 return -EINVAL;
459 }
460 if (buf_set->stream == NULL) {
461 ALOGE("%s:%d: NULL stream handle", __func__, mId);
462 return -EINVAL;
463 }
464 Stream *stream = reinterpret_cast<Stream*>(buf_set->stream->priv);
465 return stream->registerBuffers(buf_set);
Alex Raya0ed4be2013-02-25 15:02:16 -0800466}
467
468const camera_metadata_t* Camera::constructDefaultRequestSettings(int type)
469{
470 ALOGV("%s:%d: type=%d", __func__, mId, type);
Alex Ray89a82662013-05-28 20:32:48 -0700471
472 if (type < 1 || type >= CAMERA3_TEMPLATE_COUNT) {
473 ALOGE("%s:%d: Invalid template request type: %d", __func__, mId, type);
474 return NULL;
475 }
476 return mTemplates[type]->generate();
Alex Raya0ed4be2013-02-25 15:02:16 -0800477}
478
479int Camera::processCaptureRequest(camera3_capture_request_t *request)
480{
Alex Ray083315c2013-04-26 19:32:29 -0700481 camera3_capture_result result;
482
Alex Raya0ed4be2013-02-25 15:02:16 -0800483 ALOGV("%s:%d: request=%p", __func__, mId, request);
Alex Rayc16e56d2013-04-29 12:24:49 -0700484 CAMTRACE_CALL();
Alex Raya0ed4be2013-02-25 15:02:16 -0800485
486 if (request == NULL) {
487 ALOGE("%s:%d: NULL request recieved", __func__, mId);
Alex Raya0ed4be2013-02-25 15:02:16 -0800488 return -EINVAL;
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800489 }
490
Alex Raybfcbd952013-03-20 13:20:02 -0700491 ALOGV("%s:%d: Request Frame:%d Settings:%p", __func__, mId,
492 request->frame_number, request->settings);
493
494 // NULL indicates use last settings
495 if (request->settings == NULL) {
496 if (mSettings == NULL) {
497 ALOGE("%s:%d: NULL settings without previous set Frame:%d Req:%p",
498 __func__, mId, request->frame_number, request);
499 return -EINVAL;
500 }
501 } else {
502 setSettings(request->settings);
503 }
504
Alex Ray11bbeef2013-04-26 14:47:08 -0700505 if (request->input_buffer != NULL) {
506 ALOGV("%s:%d: Reprocessing input buffer %p", __func__, mId,
507 request->input_buffer);
508
509 if (!isValidReprocessSettings(request->settings)) {
510 ALOGE("%s:%d: Invalid settings for reprocess request: %p",
511 __func__, mId, request->settings);
512 return -EINVAL;
513 }
514 } else {
515 ALOGV("%s:%d: Capturing new frame.", __func__, mId);
516
517 if (!isValidCaptureSettings(request->settings)) {
518 ALOGE("%s:%d: Invalid settings for capture request: %p",
519 __func__, mId, request->settings);
520 return -EINVAL;
521 }
522 }
523
Alex Ray083315c2013-04-26 19:32:29 -0700524 if (request->num_output_buffers <= 0) {
525 ALOGE("%s:%d: Invalid number of output buffers: %d", __func__, mId,
526 request->num_output_buffers);
527 return -EINVAL;
528 }
529 result.num_output_buffers = request->num_output_buffers;
530 result.output_buffers = new camera3_stream_buffer_t[result.num_output_buffers];
531 for (unsigned int i = 0; i < request->num_output_buffers; i++) {
532 int res = processCaptureBuffer(&request->output_buffers[i],
533 const_cast<camera3_stream_buffer_t*>(&result.output_buffers[i]));
534 if (res)
535 goto err_out;
536 }
537
538 result.frame_number = request->frame_number;
539 // TODO: return actual captured/reprocessed settings
540 result.result = request->settings;
541 // TODO: asynchronously return results
542 mCallbackOps->process_capture_result(mCallbackOps, &result);
543
Alex Raya0ed4be2013-02-25 15:02:16 -0800544 return 0;
Alex Ray083315c2013-04-26 19:32:29 -0700545
546err_out:
547 delete [] result.output_buffers;
548 // TODO: this should probably be a total device failure; transient for now
549 return -EINVAL;
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800550}
551
Alex Raybfcbd952013-03-20 13:20:02 -0700552void Camera::setSettings(const camera_metadata_t *new_settings)
553{
554 if (mSettings != NULL) {
555 free_camera_metadata(mSettings);
556 mSettings = NULL;
557 }
558
559 if (new_settings != NULL)
560 mSettings = clone_camera_metadata(new_settings);
561}
562
Alex Rayc6bf2f22013-05-28 15:52:04 -0700563bool Camera::isValidCaptureSettings(const camera_metadata_t* /*settings*/)
Alex Ray11bbeef2013-04-26 14:47:08 -0700564{
565 // TODO: reject settings that cannot be captured
566 return true;
567}
568
Alex Rayc6bf2f22013-05-28 15:52:04 -0700569bool Camera::isValidReprocessSettings(const camera_metadata_t* /*settings*/)
Alex Ray11bbeef2013-04-26 14:47:08 -0700570{
571 // TODO: reject settings that cannot be reprocessed
572 // input buffers unimplemented, use this to reject reprocessing requests
573 ALOGE("%s:%d: Input buffer reprocessing not implemented", __func__, mId);
574 return false;
575}
576
Alex Ray083315c2013-04-26 19:32:29 -0700577int Camera::processCaptureBuffer(const camera3_stream_buffer_t *in,
578 camera3_stream_buffer_t *out)
579{
580 int res = sync_wait(in->acquire_fence, CAMERA_SYNC_TIMEOUT);
581 if (res == -ETIME) {
582 ALOGE("%s:%d: Timeout waiting on buffer acquire fence", __func__, mId);
583 return res;
584 } else if (res) {
585 ALOGE("%s:%d: Error waiting on buffer acquire fence: %s(%d)",
586 __func__, mId, strerror(-res), res);
587 return res;
588 }
589
590 out->stream = in->stream;
591 out->buffer = in->buffer;
592 out->status = CAMERA3_BUFFER_STATUS_OK;
593 // TODO: use driver-backed release fences
594 out->acquire_fence = -1;
595 out->release_fence = -1;
596
597 // TODO: lock and software-paint buffer
598 return 0;
599}
600
Alex Raya0ed4be2013-02-25 15:02:16 -0800601void Camera::getMetadataVendorTagOps(vendor_tag_query_ops_t *ops)
602{
603 ALOGV("%s:%d: ops=%p", __func__, mId, ops);
604 // TODO: return vendor tag ops
605}
606
607void Camera::dump(int fd)
608{
Alex Rayaf3a4612013-04-29 14:16:10 -0700609 ALOGV("%s:%d: Dumping to fd %d", __func__, mId, fd);
Alex Raya0ed4be2013-02-25 15:02:16 -0800610 // TODO: dprintf all relevant state to fd
611}
612
613extern "C" {
614// Get handle to camera from device priv data
615static Camera *camdev_to_camera(const camera3_device_t *dev)
616{
617 return reinterpret_cast<Camera*>(dev->priv);
618}
619
620static int initialize(const camera3_device_t *dev,
621 const camera3_callback_ops_t *callback_ops)
622{
623 return camdev_to_camera(dev)->initialize(callback_ops);
624}
625
626static int configure_streams(const camera3_device_t *dev,
627 camera3_stream_configuration_t *stream_list)
628{
629 return camdev_to_camera(dev)->configureStreams(stream_list);
630}
631
632static int register_stream_buffers(const camera3_device_t *dev,
633 const camera3_stream_buffer_set_t *buffer_set)
634{
635 return camdev_to_camera(dev)->registerStreamBuffers(buffer_set);
636}
637
638static const camera_metadata_t *construct_default_request_settings(
639 const camera3_device_t *dev, int type)
640{
641 return camdev_to_camera(dev)->constructDefaultRequestSettings(type);
642}
643
644static int process_capture_request(const camera3_device_t *dev,
645 camera3_capture_request_t *request)
646{
647 return camdev_to_camera(dev)->processCaptureRequest(request);
648}
649
650static void get_metadata_vendor_tag_ops(const camera3_device_t *dev,
651 vendor_tag_query_ops_t *ops)
652{
653 camdev_to_camera(dev)->getMetadataVendorTagOps(ops);
654}
655
656static void dump(const camera3_device_t *dev, int fd)
657{
658 camdev_to_camera(dev)->dump(fd);
659}
660} // extern "C"
661
662const camera3_device_ops_t Camera::sOps = {
663 .initialize = default_camera_hal::initialize,
664 .configure_streams = default_camera_hal::configure_streams,
665 .register_stream_buffers = default_camera_hal::register_stream_buffers,
666 .construct_default_request_settings =
667 default_camera_hal::construct_default_request_settings,
668 .process_capture_request = default_camera_hal::process_capture_request,
669 .get_metadata_vendor_tag_ops =
670 default_camera_hal::get_metadata_vendor_tag_ops,
671 .dump = default_camera_hal::dump
672};
673
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800674} // namespace default_camera_hal