blob: 724f8617fce883f42af93c07e0d021c5fbcc203e [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)
Alex Rayea803822013-10-14 15:56:43 -070032#include <utils/Trace.h>
Alex Rayed6b8a72012-12-27 10:42:22 -080033
Alex Ray7ee0b7a2012-11-06 00:12:49 -080034#include "Camera.h"
35
Alex Ray083315c2013-04-26 19:32:29 -070036#define CAMERA_SYNC_TIMEOUT 5000 // in msecs
37
Alex Rayb0be1032013-05-28 15:52:47 -070038#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
39
Alex Ray7ee0b7a2012-11-06 00:12:49 -080040namespace default_camera_hal {
41
42extern "C" {
43// Shim passed to the framework to close an opened device.
44static int close_device(hw_device_t* dev)
45{
Alex Raya0ed4be2013-02-25 15:02:16 -080046 camera3_device_t* cam_dev = reinterpret_cast<camera3_device_t*>(dev);
Alex Ray7ee0b7a2012-11-06 00:12:49 -080047 Camera* cam = static_cast<Camera*>(cam_dev->priv);
48 return cam->close();
49}
50} // extern "C"
51
Alex Raya0ed4be2013-02-25 15:02:16 -080052Camera::Camera(int id)
53 : mId(id),
Alex Rayb0be1032013-05-28 15:52:47 -070054 mStaticInfo(NULL),
Alex Raya0ed4be2013-02-25 15:02:16 -080055 mBusy(false),
Alex Raybcaf7882013-02-28 16:04:35 -080056 mCallbackOps(NULL),
57 mStreams(NULL),
Alex Raybfcbd952013-03-20 13:20:02 -070058 mNumStreams(0),
59 mSettings(NULL)
Alex Ray7ee0b7a2012-11-06 00:12:49 -080060{
Alex Ray0f82f5a2013-07-17 14:23:04 -070061 pthread_mutex_init(&mMutex, NULL);
62 pthread_mutex_init(&mStaticInfoMutex, NULL);
Alex Ray7ee0b7a2012-11-06 00:12:49 -080063
Alex Raya0ed4be2013-02-25 15:02:16 -080064 memset(&mDevice, 0, sizeof(mDevice));
Alex Ray7ee0b7a2012-11-06 00:12:49 -080065 mDevice.common.tag = HARDWARE_DEVICE_TAG;
Alex Rayb0be1032013-05-28 15:52:47 -070066 mDevice.common.version = CAMERA_DEVICE_API_VERSION_3_0;
Alex Ray7ee0b7a2012-11-06 00:12:49 -080067 mDevice.common.close = close_device;
Alex Raya0ed4be2013-02-25 15:02:16 -080068 mDevice.ops = const_cast<camera3_device_ops_t*>(&sOps);
Alex Ray7ee0b7a2012-11-06 00:12:49 -080069 mDevice.priv = this;
70}
71
72Camera::~Camera()
73{
Alex Ray0f82f5a2013-07-17 14:23:04 -070074 pthread_mutex_destroy(&mMutex);
75 pthread_mutex_destroy(&mStaticInfoMutex);
Alex Ray7ee0b7a2012-11-06 00:12:49 -080076}
77
Alex Raya0ed4be2013-02-25 15:02:16 -080078int Camera::open(const hw_module_t *module, hw_device_t **device)
Alex Ray7ee0b7a2012-11-06 00:12:49 -080079{
Alex Raya0ed4be2013-02-25 15:02:16 -080080 ALOGI("%s:%d: Opening camera device", __func__, mId);
Alex Rayea803822013-10-14 15:56:43 -070081 ATRACE_CALL();
Alex Ray7ee0b7a2012-11-06 00:12:49 -080082 pthread_mutex_lock(&mMutex);
83 if (mBusy) {
84 pthread_mutex_unlock(&mMutex);
Alex Raya0ed4be2013-02-25 15:02:16 -080085 ALOGE("%s:%d: Error! Camera device already opened", __func__, mId);
Alex Ray7ee0b7a2012-11-06 00:12:49 -080086 return -EBUSY;
87 }
88
89 // TODO: open camera dev nodes, etc
90 mBusy = true;
Alex Raya0ed4be2013-02-25 15:02:16 -080091 mDevice.common.module = const_cast<hw_module_t*>(module);
92 *device = &mDevice.common;
Alex Ray7ee0b7a2012-11-06 00:12:49 -080093
94 pthread_mutex_unlock(&mMutex);
95 return 0;
96}
97
Alex Rayb0be1032013-05-28 15:52:47 -070098int Camera::getInfo(struct camera_info *info)
99{
Alex Rayb0be1032013-05-28 15:52:47 -0700100 info->facing = CAMERA_FACING_FRONT;
101 info->orientation = 0;
102 info->device_version = mDevice.common.version;
103
Alex Ray0f82f5a2013-07-17 14:23:04 -0700104 pthread_mutex_lock(&mStaticInfoMutex);
Alex Rayb0be1032013-05-28 15:52:47 -0700105 if (mStaticInfo == NULL) {
Alex Ray0f82f5a2013-07-17 14:23:04 -0700106 mStaticInfo = initStaticInfo();
Alex Rayb0be1032013-05-28 15:52:47 -0700107 }
Alex Ray0f82f5a2013-07-17 14:23:04 -0700108 pthread_mutex_unlock(&mStaticInfoMutex);
Alex Rayb0be1032013-05-28 15:52:47 -0700109
110 info->static_camera_characteristics = mStaticInfo;
111
Alex Ray0f82f5a2013-07-17 14:23:04 -0700112 return 0;
Alex Rayb0be1032013-05-28 15:52:47 -0700113}
114
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800115int Camera::close()
116{
Alex Raya0ed4be2013-02-25 15:02:16 -0800117 ALOGI("%s:%d: Closing camera device", __func__, mId);
Alex Rayea803822013-10-14 15:56:43 -0700118 ATRACE_CALL();
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800119 pthread_mutex_lock(&mMutex);
120 if (!mBusy) {
121 pthread_mutex_unlock(&mMutex);
Alex Raya0ed4be2013-02-25 15:02:16 -0800122 ALOGE("%s:%d: Error! Camera device not open", __func__, mId);
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800123 return -EINVAL;
124 }
125
126 // TODO: close camera dev nodes, etc
127 mBusy = false;
128
129 pthread_mutex_unlock(&mMutex);
130 return 0;
131}
132
Alex Raya0ed4be2013-02-25 15:02:16 -0800133int Camera::initialize(const camera3_callback_ops_t *callback_ops)
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800134{
Alex Raya0ed4be2013-02-25 15:02:16 -0800135 ALOGV("%s:%d: callback_ops=%p", __func__, mId, callback_ops);
136 mCallbackOps = callback_ops;
Alex Ray89a82662013-05-28 20:32:48 -0700137 // Create standard settings templates
138 // 0 is invalid as template
139 mTemplates[0] = NULL;
140 // CAMERA3_TEMPLATE_PREVIEW = 1
141 mTemplates[1] = new Metadata(ANDROID_CONTROL_MODE_OFF,
142 ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW);
143 // CAMERA3_TEMPLATE_STILL_CAPTURE = 2
144 mTemplates[2] = new Metadata(ANDROID_CONTROL_MODE_OFF,
145 ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE);
146 // CAMERA3_TEMPLATE_VIDEO_RECORD = 3
147 mTemplates[3] = new Metadata(ANDROID_CONTROL_MODE_OFF,
148 ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD);
149 // CAMERA3_TEMPLATE_VIDEO_SNAPSHOT = 4
150 mTemplates[4] = new Metadata(ANDROID_CONTROL_MODE_OFF,
151 ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT);
152 // CAMERA3_TEMPLATE_STILL_ZERO_SHUTTER_LAG = 5
153 mTemplates[5] = new Metadata(ANDROID_CONTROL_MODE_OFF,
154 ANDROID_CONTROL_CAPTURE_INTENT_ZERO_SHUTTER_LAG);
155 // Pre-generate metadata structures
156 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
157 mTemplates[i]->generate();
158 }
159 // TODO: create vendor templates
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800160 return 0;
161}
162
Alex Ray0f82f5a2013-07-17 14:23:04 -0700163camera_metadata_t *Camera::initStaticInfo()
Alex Rayb0be1032013-05-28 15:52:47 -0700164{
165 /*
166 * Setup static camera info. This will have to customized per camera
167 * device.
168 */
Alex Ray0f82f5a2013-07-17 14:23:04 -0700169 Metadata m;
Alex Rayb0be1032013-05-28 15:52:47 -0700170
171 /* android.control */
172 int32_t android_control_ae_available_target_fps_ranges[] = {30, 30};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700173 m.addInt32(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES,
Alex Rayb0be1032013-05-28 15:52:47 -0700174 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};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700178 m.addInt32(ANDROID_CONTROL_AE_COMPENSATION_RANGE,
Alex Rayb0be1032013-05-28 15:52:47 -0700179 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}};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700183 m.addRational(ANDROID_CONTROL_AE_COMPENSATION_STEP,
Alex Rayb0be1032013-05-28 15:52:47 -0700184 ARRAY_SIZE(android_control_ae_compensation_step),
185 android_control_ae_compensation_step);
186
187 int32_t android_control_max_regions[] = {1};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700188 m.addInt32(ANDROID_CONTROL_MAX_REGIONS,
Alex Rayb0be1032013-05-28 15:52:47 -0700189 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};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700194 m.addInt32(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES,
Alex Rayb0be1032013-05-28 15:52:47 -0700195 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};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700200 m.addFloat(ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS,
Alex Rayb0be1032013-05-28 15:52:47 -0700201 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};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700206 m.addInt32(ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS,
Alex Rayb0be1032013-05-28 15:52:47 -0700207 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};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700220 m.addInt32(ANDROID_SCALER_AVAILABLE_FORMATS,
Alex Rayb0be1032013-05-28 15:52:47 -0700221 ARRAY_SIZE(android_scaler_available_formats),
222 android_scaler_available_formats);
223
224 int64_t android_scaler_available_jpeg_min_durations[] = {1};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700225 m.addInt64(ANDROID_SCALER_AVAILABLE_JPEG_MIN_DURATIONS,
Alex Rayb0be1032013-05-28 15:52:47 -0700226 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};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700230 m.addInt32(ANDROID_SCALER_AVAILABLE_JPEG_SIZES,
Alex Rayb0be1032013-05-28 15:52:47 -0700231 ARRAY_SIZE(android_scaler_available_jpeg_sizes),
232 android_scaler_available_jpeg_sizes);
233
234 float android_scaler_available_max_digital_zoom[] = {1};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700235 m.addFloat(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
Alex Rayb0be1032013-05-28 15:52:47 -0700236 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};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700240 m.addInt64(ANDROID_SCALER_AVAILABLE_PROCESSED_MIN_DURATIONS,
Alex Rayb0be1032013-05-28 15:52:47 -0700241 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};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700245 m.addInt32(ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES,
Alex Rayb0be1032013-05-28 15:52:47 -0700246 ARRAY_SIZE(android_scaler_available_processed_sizes),
247 android_scaler_available_processed_sizes);
248
249 int64_t android_scaler_available_raw_min_durations[] = {1};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700250 m.addInt64(ANDROID_SCALER_AVAILABLE_RAW_MIN_DURATIONS,
Alex Rayb0be1032013-05-28 15:52:47 -0700251 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};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700255 m.addInt32(ANDROID_SCALER_AVAILABLE_RAW_SIZES,
Alex Rayb0be1032013-05-28 15:52:47 -0700256 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};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700262 m.addInt32(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE,
Alex Rayb0be1032013-05-28 15:52:47 -0700263 ARRAY_SIZE(android_sensor_info_active_array_size),
264 android_sensor_info_active_array_size);
265
Zhijun Hebd146892013-07-18 17:59:30 -0700266 int32_t android_sensor_info_sensitivity_range[] =
267 {100, 1600};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700268 m.addInt32(ANDROID_SENSOR_INFO_SENSITIVITY_RANGE,
Zhijun Hebd146892013-07-18 17:59:30 -0700269 ARRAY_SIZE(android_sensor_info_sensitivity_range),
270 android_sensor_info_sensitivity_range);
Alex Rayb0be1032013-05-28 15:52:47 -0700271
272 int64_t android_sensor_info_max_frame_duration[] = {30000000000};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700273 m.addInt64(ANDROID_SENSOR_INFO_MAX_FRAME_DURATION,
Alex Rayb0be1032013-05-28 15:52:47 -0700274 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};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700278 m.addFloat(ANDROID_SENSOR_INFO_PHYSICAL_SIZE,
Alex Rayb0be1032013-05-28 15:52:47 -0700279 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};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700283 m.addInt32(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE,
Alex Rayb0be1032013-05-28 15:52:47 -0700284 ARRAY_SIZE(android_sensor_info_pixel_array_size),
285 android_sensor_info_pixel_array_size);
286
287 int32_t android_sensor_orientation[] = {0};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700288 m.addInt32(ANDROID_SENSOR_ORIENTATION,
Alex Rayb0be1032013-05-28 15:52:47 -0700289 ARRAY_SIZE(android_sensor_orientation),
290 android_sensor_orientation);
291
292 /* End of static camera characteristics */
293
Alex Ray0f82f5a2013-07-17 14:23:04 -0700294 return clone_camera_metadata(m.generate());
Alex Rayb0be1032013-05-28 15:52:47 -0700295}
296
Alex Raybcaf7882013-02-28 16:04:35 -0800297int Camera::configureStreams(camera3_stream_configuration_t *stream_config)
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800298{
Alex Raybcaf7882013-02-28 16:04:35 -0800299 camera3_stream_t *astream;
300 Stream **newStreams = NULL;
301
Alex Rayea803822013-10-14 15:56:43 -0700302 ATRACE_CALL();
Alex Raybcaf7882013-02-28 16:04:35 -0800303 ALOGV("%s:%d: stream_config=%p", __func__, mId, stream_config);
304
305 if (stream_config == NULL) {
306 ALOGE("%s:%d: NULL stream configuration array", __func__, mId);
307 return -EINVAL;
308 }
309 if (stream_config->num_streams == 0) {
310 ALOGE("%s:%d: Empty stream configuration array", __func__, mId);
311 return -EINVAL;
312 }
313
314 // Create new stream array
315 newStreams = new Stream*[stream_config->num_streams];
316 ALOGV("%s:%d: Number of Streams: %d", __func__, mId,
317 stream_config->num_streams);
318
319 pthread_mutex_lock(&mMutex);
320
321 // Mark all current streams unused for now
322 for (int i = 0; i < mNumStreams; i++)
323 mStreams[i]->mReuse = false;
324 // Fill new stream array with reused streams and new streams
Alex Rayc6bf2f22013-05-28 15:52:04 -0700325 for (unsigned int i = 0; i < stream_config->num_streams; i++) {
Alex Raybcaf7882013-02-28 16:04:35 -0800326 astream = stream_config->streams[i];
Alex Ray2b286da2013-05-29 15:08:29 -0700327 if (astream->max_buffers > 0) {
328 ALOGV("%s:%d: Reusing stream %d", __func__, mId, i);
Alex Raybcaf7882013-02-28 16:04:35 -0800329 newStreams[i] = reuseStream(astream);
Alex Ray2b286da2013-05-29 15:08:29 -0700330 } else {
331 ALOGV("%s:%d: Creating new stream %d", __func__, mId, i);
Alex Raybcaf7882013-02-28 16:04:35 -0800332 newStreams[i] = new Stream(mId, astream);
Alex Ray2b286da2013-05-29 15:08:29 -0700333 }
Alex Raybcaf7882013-02-28 16:04:35 -0800334
335 if (newStreams[i] == NULL) {
336 ALOGE("%s:%d: Error processing stream %d", __func__, mId, i);
337 goto err_out;
338 }
339 astream->priv = newStreams[i];
340 }
341
342 // Verify the set of streams in aggregate
343 if (!isValidStreamSet(newStreams, stream_config->num_streams)) {
344 ALOGE("%s:%d: Invalid stream set", __func__, mId);
345 goto err_out;
346 }
347
348 // Set up all streams (calculate usage/max_buffers for each)
349 setupStreams(newStreams, stream_config->num_streams);
350
351 // Destroy all old streams and replace stream array with new one
352 destroyStreams(mStreams, mNumStreams);
353 mStreams = newStreams;
354 mNumStreams = stream_config->num_streams;
355
Alex Raybfcbd952013-03-20 13:20:02 -0700356 // Clear out last seen settings metadata
357 setSettings(NULL);
358
Alex Raybcaf7882013-02-28 16:04:35 -0800359 pthread_mutex_unlock(&mMutex);
Alex Raya0ed4be2013-02-25 15:02:16 -0800360 return 0;
Alex Raybcaf7882013-02-28 16:04:35 -0800361
362err_out:
363 // Clean up temporary streams, preserve existing mStreams/mNumStreams
364 destroyStreams(newStreams, stream_config->num_streams);
365 pthread_mutex_unlock(&mMutex);
366 return -EINVAL;
367}
368
369void Camera::destroyStreams(Stream **streams, int count)
370{
371 if (streams == NULL)
372 return;
373 for (int i = 0; i < count; i++) {
374 // Only destroy streams that weren't reused
375 if (streams[i] != NULL && !streams[i]->mReuse)
376 delete streams[i];
377 }
378 delete [] streams;
379}
380
381Stream *Camera::reuseStream(camera3_stream_t *astream)
382{
383 Stream *priv = reinterpret_cast<Stream*>(astream->priv);
384 // Verify the re-used stream's parameters match
385 if (!priv->isValidReuseStream(mId, astream)) {
386 ALOGE("%s:%d: Mismatched parameter in reused stream", __func__, mId);
387 return NULL;
388 }
389 // Mark stream to be reused
390 priv->mReuse = true;
391 return priv;
392}
393
394bool Camera::isValidStreamSet(Stream **streams, int count)
395{
396 int inputs = 0;
397 int outputs = 0;
398
399 if (streams == NULL) {
400 ALOGE("%s:%d: NULL stream configuration streams", __func__, mId);
401 return false;
402 }
403 if (count == 0) {
404 ALOGE("%s:%d: Zero count stream configuration streams", __func__, mId);
405 return false;
406 }
407 // Validate there is at most one input stream and at least one output stream
408 for (int i = 0; i < count; i++) {
409 // A stream may be both input and output (bidirectional)
410 if (streams[i]->isInputType())
411 inputs++;
412 if (streams[i]->isOutputType())
413 outputs++;
414 }
Alex Ray768216e2013-07-02 16:56:14 -0700415 ALOGV("%s:%d: Configuring %d output streams and %d input streams",
416 __func__, mId, outputs, inputs);
Alex Raybcaf7882013-02-28 16:04:35 -0800417 if (outputs < 1) {
418 ALOGE("%s:%d: Stream config must have >= 1 output", __func__, mId);
419 return false;
420 }
421 if (inputs > 1) {
422 ALOGE("%s:%d: Stream config must have <= 1 input", __func__, mId);
423 return false;
424 }
425 // TODO: check for correct number of Bayer/YUV/JPEG/Encoder streams
426 return true;
427}
428
429void Camera::setupStreams(Stream **streams, int count)
430{
431 /*
432 * This is where the HAL has to decide internally how to handle all of the
433 * streams, and then produce usage and max_buffer values for each stream.
434 * Note, the stream array has been checked before this point for ALL invalid
435 * conditions, so it must find a successful configuration for this stream
436 * array. The HAL may not return an error from this point.
437 *
438 * In this demo HAL, we just set all streams to be the same dummy values;
439 * real implementations will want to avoid USAGE_SW_{READ|WRITE}_OFTEN.
440 */
441 for (int i = 0; i < count; i++) {
442 uint32_t usage = 0;
443
444 if (streams[i]->isOutputType())
445 usage |= GRALLOC_USAGE_SW_WRITE_OFTEN |
446 GRALLOC_USAGE_HW_CAMERA_WRITE;
447 if (streams[i]->isInputType())
448 usage |= GRALLOC_USAGE_SW_READ_OFTEN |
449 GRALLOC_USAGE_HW_CAMERA_READ;
450
451 streams[i]->setUsage(usage);
452 streams[i]->setMaxBuffers(1);
453 }
Alex Raya0ed4be2013-02-25 15:02:16 -0800454}
455
456int Camera::registerStreamBuffers(const camera3_stream_buffer_set_t *buf_set)
457{
458 ALOGV("%s:%d: buffer_set=%p", __func__, mId, buf_set);
Alex Ray8a8f86b2013-03-01 01:32:21 -0800459 if (buf_set == NULL) {
460 ALOGE("%s:%d: NULL buffer set", __func__, mId);
461 return -EINVAL;
462 }
463 if (buf_set->stream == NULL) {
464 ALOGE("%s:%d: NULL stream handle", __func__, mId);
465 return -EINVAL;
466 }
467 Stream *stream = reinterpret_cast<Stream*>(buf_set->stream->priv);
468 return stream->registerBuffers(buf_set);
Alex Raya0ed4be2013-02-25 15:02:16 -0800469}
470
471const camera_metadata_t* Camera::constructDefaultRequestSettings(int type)
472{
473 ALOGV("%s:%d: type=%d", __func__, mId, type);
Alex Ray89a82662013-05-28 20:32:48 -0700474
475 if (type < 1 || type >= CAMERA3_TEMPLATE_COUNT) {
476 ALOGE("%s:%d: Invalid template request type: %d", __func__, mId, type);
477 return NULL;
478 }
479 return mTemplates[type]->generate();
Alex Raya0ed4be2013-02-25 15:02:16 -0800480}
481
482int Camera::processCaptureRequest(camera3_capture_request_t *request)
483{
Alex Ray083315c2013-04-26 19:32:29 -0700484 camera3_capture_result result;
485
Alex Raya0ed4be2013-02-25 15:02:16 -0800486 ALOGV("%s:%d: request=%p", __func__, mId, request);
Alex Rayea803822013-10-14 15:56:43 -0700487 ATRACE_CALL();
Alex Raya0ed4be2013-02-25 15:02:16 -0800488
489 if (request == NULL) {
490 ALOGE("%s:%d: NULL request recieved", __func__, mId);
Alex Raya0ed4be2013-02-25 15:02:16 -0800491 return -EINVAL;
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800492 }
493
Alex Raybfcbd952013-03-20 13:20:02 -0700494 ALOGV("%s:%d: Request Frame:%d Settings:%p", __func__, mId,
495 request->frame_number, request->settings);
496
497 // NULL indicates use last settings
498 if (request->settings == NULL) {
499 if (mSettings == NULL) {
500 ALOGE("%s:%d: NULL settings without previous set Frame:%d Req:%p",
501 __func__, mId, request->frame_number, request);
502 return -EINVAL;
503 }
504 } else {
505 setSettings(request->settings);
506 }
507
Alex Ray11bbeef2013-04-26 14:47:08 -0700508 if (request->input_buffer != NULL) {
509 ALOGV("%s:%d: Reprocessing input buffer %p", __func__, mId,
510 request->input_buffer);
511
512 if (!isValidReprocessSettings(request->settings)) {
513 ALOGE("%s:%d: Invalid settings for reprocess request: %p",
514 __func__, mId, request->settings);
515 return -EINVAL;
516 }
517 } else {
518 ALOGV("%s:%d: Capturing new frame.", __func__, mId);
519
520 if (!isValidCaptureSettings(request->settings)) {
521 ALOGE("%s:%d: Invalid settings for capture request: %p",
522 __func__, mId, request->settings);
523 return -EINVAL;
524 }
525 }
526
Alex Ray083315c2013-04-26 19:32:29 -0700527 if (request->num_output_buffers <= 0) {
528 ALOGE("%s:%d: Invalid number of output buffers: %d", __func__, mId,
529 request->num_output_buffers);
530 return -EINVAL;
531 }
532 result.num_output_buffers = request->num_output_buffers;
533 result.output_buffers = new camera3_stream_buffer_t[result.num_output_buffers];
534 for (unsigned int i = 0; i < request->num_output_buffers; i++) {
535 int res = processCaptureBuffer(&request->output_buffers[i],
536 const_cast<camera3_stream_buffer_t*>(&result.output_buffers[i]));
537 if (res)
538 goto err_out;
539 }
540
541 result.frame_number = request->frame_number;
542 // TODO: return actual captured/reprocessed settings
543 result.result = request->settings;
544 // TODO: asynchronously return results
Alex Ray764e4422013-06-04 12:38:07 -0700545 notifyShutter(request->frame_number, 0);
Alex Ray083315c2013-04-26 19:32:29 -0700546 mCallbackOps->process_capture_result(mCallbackOps, &result);
547
Alex Raya0ed4be2013-02-25 15:02:16 -0800548 return 0;
Alex Ray083315c2013-04-26 19:32:29 -0700549
550err_out:
551 delete [] result.output_buffers;
552 // TODO: this should probably be a total device failure; transient for now
553 return -EINVAL;
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800554}
555
Alex Raybfcbd952013-03-20 13:20:02 -0700556void Camera::setSettings(const camera_metadata_t *new_settings)
557{
558 if (mSettings != NULL) {
559 free_camera_metadata(mSettings);
560 mSettings = NULL;
561 }
562
563 if (new_settings != NULL)
564 mSettings = clone_camera_metadata(new_settings);
565}
566
Alex Rayc6bf2f22013-05-28 15:52:04 -0700567bool Camera::isValidCaptureSettings(const camera_metadata_t* /*settings*/)
Alex Ray11bbeef2013-04-26 14:47:08 -0700568{
569 // TODO: reject settings that cannot be captured
570 return true;
571}
572
Alex Rayc6bf2f22013-05-28 15:52:04 -0700573bool Camera::isValidReprocessSettings(const camera_metadata_t* /*settings*/)
Alex Ray11bbeef2013-04-26 14:47:08 -0700574{
575 // TODO: reject settings that cannot be reprocessed
576 // input buffers unimplemented, use this to reject reprocessing requests
577 ALOGE("%s:%d: Input buffer reprocessing not implemented", __func__, mId);
578 return false;
579}
580
Alex Ray083315c2013-04-26 19:32:29 -0700581int Camera::processCaptureBuffer(const camera3_stream_buffer_t *in,
582 camera3_stream_buffer_t *out)
583{
Alex Ray77ecfd72013-05-30 00:19:04 -0700584 if (in->acquire_fence != -1) {
585 int res = sync_wait(in->acquire_fence, CAMERA_SYNC_TIMEOUT);
586 if (res == -ETIME) {
587 ALOGE("%s:%d: Timeout waiting on buffer acquire fence",
588 __func__, mId);
589 return res;
590 } else if (res) {
591 ALOGE("%s:%d: Error waiting on buffer acquire fence: %s(%d)",
592 __func__, mId, strerror(-res), res);
593 return res;
594 }
Alex Ray083315c2013-04-26 19:32:29 -0700595 }
596
597 out->stream = in->stream;
598 out->buffer = in->buffer;
599 out->status = CAMERA3_BUFFER_STATUS_OK;
600 // TODO: use driver-backed release fences
601 out->acquire_fence = -1;
602 out->release_fence = -1;
603
604 // TODO: lock and software-paint buffer
605 return 0;
606}
607
Alex Ray764e4422013-06-04 12:38:07 -0700608void Camera::notifyShutter(uint32_t frame_number, uint64_t timestamp)
609{
610 int res;
611 struct timespec ts;
612
613 // If timestamp is 0, get timestamp from right now instead
614 if (timestamp == 0) {
615 ALOGW("%s:%d: No timestamp provided, using CLOCK_BOOTTIME",
616 __func__, mId);
617 res = clock_gettime(CLOCK_BOOTTIME, &ts);
618 if (res == 0) {
619 timestamp = ts.tv_sec * 1000000000ULL + ts.tv_nsec;
620 } else {
621 ALOGE("%s:%d: No timestamp and failed to get CLOCK_BOOTTIME %s(%d)",
622 __func__, mId, strerror(errno), errno);
623 }
624 }
625 camera3_notify_msg_t m;
626 memset(&m, 0, sizeof(m));
627 m.type = CAMERA3_MSG_SHUTTER;
628 m.message.shutter.frame_number = frame_number;
629 m.message.shutter.timestamp = timestamp;
630 mCallbackOps->notify(mCallbackOps, &m);
631}
632
Alex Raya0ed4be2013-02-25 15:02:16 -0800633void Camera::getMetadataVendorTagOps(vendor_tag_query_ops_t *ops)
634{
635 ALOGV("%s:%d: ops=%p", __func__, mId, ops);
636 // TODO: return vendor tag ops
637}
638
639void Camera::dump(int fd)
640{
Alex Rayaf3a4612013-04-29 14:16:10 -0700641 ALOGV("%s:%d: Dumping to fd %d", __func__, mId, fd);
Alex Raya0ed4be2013-02-25 15:02:16 -0800642 // TODO: dprintf all relevant state to fd
643}
644
645extern "C" {
646// Get handle to camera from device priv data
647static Camera *camdev_to_camera(const camera3_device_t *dev)
648{
649 return reinterpret_cast<Camera*>(dev->priv);
650}
651
652static int initialize(const camera3_device_t *dev,
653 const camera3_callback_ops_t *callback_ops)
654{
655 return camdev_to_camera(dev)->initialize(callback_ops);
656}
657
658static int configure_streams(const camera3_device_t *dev,
659 camera3_stream_configuration_t *stream_list)
660{
661 return camdev_to_camera(dev)->configureStreams(stream_list);
662}
663
664static int register_stream_buffers(const camera3_device_t *dev,
665 const camera3_stream_buffer_set_t *buffer_set)
666{
667 return camdev_to_camera(dev)->registerStreamBuffers(buffer_set);
668}
669
670static const camera_metadata_t *construct_default_request_settings(
671 const camera3_device_t *dev, int type)
672{
673 return camdev_to_camera(dev)->constructDefaultRequestSettings(type);
674}
675
676static int process_capture_request(const camera3_device_t *dev,
677 camera3_capture_request_t *request)
678{
679 return camdev_to_camera(dev)->processCaptureRequest(request);
680}
681
682static void get_metadata_vendor_tag_ops(const camera3_device_t *dev,
683 vendor_tag_query_ops_t *ops)
684{
685 camdev_to_camera(dev)->getMetadataVendorTagOps(ops);
686}
687
688static void dump(const camera3_device_t *dev, int fd)
689{
690 camdev_to_camera(dev)->dump(fd);
691}
692} // extern "C"
693
694const camera3_device_ops_t Camera::sOps = {
695 .initialize = default_camera_hal::initialize,
696 .configure_streams = default_camera_hal::configure_streams,
697 .register_stream_buffers = default_camera_hal::register_stream_buffers,
698 .construct_default_request_settings =
699 default_camera_hal::construct_default_request_settings,
700 .process_capture_request = default_camera_hal::process_capture_request,
701 .get_metadata_vendor_tag_ops =
702 default_camera_hal::get_metadata_vendor_tag_ops,
703 .dump = default_camera_hal::dump
704};
705
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800706} // namespace default_camera_hal