blob: 764bb8fcf2ce196fc4907d24091b6753003f26e9 [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
Alex Ray3e8a9fe2013-10-14 15:58:02 -0700198 int32_t android_jpeg_max_size[] = {13 * 1024 * 1024}; // 13MB
199 m.addInt32(ANDROID_JPEG_MAX_SIZE,
200 ARRAY_SIZE(android_jpeg_max_size),
201 android_jpeg_max_size);
202
Alex Rayb0be1032013-05-28 15:52:47 -0700203 /* android.lens */
204 float android_lens_info_available_focal_lengths[] = {1.0};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700205 m.addFloat(ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS,
Alex Rayb0be1032013-05-28 15:52:47 -0700206 ARRAY_SIZE(android_lens_info_available_focal_lengths),
207 android_lens_info_available_focal_lengths);
208
209 /* android.request */
210 int32_t android_request_max_num_output_streams[] = {0, 3, 1};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700211 m.addInt32(ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS,
Alex Rayb0be1032013-05-28 15:52:47 -0700212 ARRAY_SIZE(android_request_max_num_output_streams),
213 android_request_max_num_output_streams);
214
215 /* android.scaler */
216 int32_t android_scaler_available_formats[] = {
217 HAL_PIXEL_FORMAT_RAW_SENSOR,
218 HAL_PIXEL_FORMAT_BLOB,
219 HAL_PIXEL_FORMAT_RGBA_8888,
220 HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED,
221 // These are handled by YCbCr_420_888
222 // HAL_PIXEL_FORMAT_YV12,
223 // HAL_PIXEL_FORMAT_YCrCb_420_SP,
224 HAL_PIXEL_FORMAT_YCbCr_420_888};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700225 m.addInt32(ANDROID_SCALER_AVAILABLE_FORMATS,
Alex Rayb0be1032013-05-28 15:52:47 -0700226 ARRAY_SIZE(android_scaler_available_formats),
227 android_scaler_available_formats);
228
229 int64_t android_scaler_available_jpeg_min_durations[] = {1};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700230 m.addInt64(ANDROID_SCALER_AVAILABLE_JPEG_MIN_DURATIONS,
Alex Rayb0be1032013-05-28 15:52:47 -0700231 ARRAY_SIZE(android_scaler_available_jpeg_min_durations),
232 android_scaler_available_jpeg_min_durations);
233
234 int32_t android_scaler_available_jpeg_sizes[] = {640, 480};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700235 m.addInt32(ANDROID_SCALER_AVAILABLE_JPEG_SIZES,
Alex Rayb0be1032013-05-28 15:52:47 -0700236 ARRAY_SIZE(android_scaler_available_jpeg_sizes),
237 android_scaler_available_jpeg_sizes);
238
239 float android_scaler_available_max_digital_zoom[] = {1};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700240 m.addFloat(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
Alex Rayb0be1032013-05-28 15:52:47 -0700241 ARRAY_SIZE(android_scaler_available_max_digital_zoom),
242 android_scaler_available_max_digital_zoom);
243
244 int64_t android_scaler_available_processed_min_durations[] = {1};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700245 m.addInt64(ANDROID_SCALER_AVAILABLE_PROCESSED_MIN_DURATIONS,
Alex Rayb0be1032013-05-28 15:52:47 -0700246 ARRAY_SIZE(android_scaler_available_processed_min_durations),
247 android_scaler_available_processed_min_durations);
248
249 int32_t android_scaler_available_processed_sizes[] = {640, 480};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700250 m.addInt32(ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES,
Alex Rayb0be1032013-05-28 15:52:47 -0700251 ARRAY_SIZE(android_scaler_available_processed_sizes),
252 android_scaler_available_processed_sizes);
253
254 int64_t android_scaler_available_raw_min_durations[] = {1};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700255 m.addInt64(ANDROID_SCALER_AVAILABLE_RAW_MIN_DURATIONS,
Alex Rayb0be1032013-05-28 15:52:47 -0700256 ARRAY_SIZE(android_scaler_available_raw_min_durations),
257 android_scaler_available_raw_min_durations);
258
259 int32_t android_scaler_available_raw_sizes[] = {640, 480};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700260 m.addInt32(ANDROID_SCALER_AVAILABLE_RAW_SIZES,
Alex Rayb0be1032013-05-28 15:52:47 -0700261 ARRAY_SIZE(android_scaler_available_raw_sizes),
262 android_scaler_available_raw_sizes);
263
264 /* android.sensor */
265
266 int32_t android_sensor_info_active_array_size[] = {0, 0, 640, 480};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700267 m.addInt32(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE,
Alex Rayb0be1032013-05-28 15:52:47 -0700268 ARRAY_SIZE(android_sensor_info_active_array_size),
269 android_sensor_info_active_array_size);
270
Zhijun Hebd146892013-07-18 17:59:30 -0700271 int32_t android_sensor_info_sensitivity_range[] =
272 {100, 1600};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700273 m.addInt32(ANDROID_SENSOR_INFO_SENSITIVITY_RANGE,
Zhijun Hebd146892013-07-18 17:59:30 -0700274 ARRAY_SIZE(android_sensor_info_sensitivity_range),
275 android_sensor_info_sensitivity_range);
Alex Rayb0be1032013-05-28 15:52:47 -0700276
277 int64_t android_sensor_info_max_frame_duration[] = {30000000000};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700278 m.addInt64(ANDROID_SENSOR_INFO_MAX_FRAME_DURATION,
Alex Rayb0be1032013-05-28 15:52:47 -0700279 ARRAY_SIZE(android_sensor_info_max_frame_duration),
280 android_sensor_info_max_frame_duration);
281
282 float android_sensor_info_physical_size[] = {3.2, 2.4};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700283 m.addFloat(ANDROID_SENSOR_INFO_PHYSICAL_SIZE,
Alex Rayb0be1032013-05-28 15:52:47 -0700284 ARRAY_SIZE(android_sensor_info_physical_size),
285 android_sensor_info_physical_size);
286
287 int32_t android_sensor_info_pixel_array_size[] = {640, 480};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700288 m.addInt32(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE,
Alex Rayb0be1032013-05-28 15:52:47 -0700289 ARRAY_SIZE(android_sensor_info_pixel_array_size),
290 android_sensor_info_pixel_array_size);
291
292 int32_t android_sensor_orientation[] = {0};
Alex Ray0f82f5a2013-07-17 14:23:04 -0700293 m.addInt32(ANDROID_SENSOR_ORIENTATION,
Alex Rayb0be1032013-05-28 15:52:47 -0700294 ARRAY_SIZE(android_sensor_orientation),
295 android_sensor_orientation);
296
297 /* End of static camera characteristics */
298
Alex Ray0f82f5a2013-07-17 14:23:04 -0700299 return clone_camera_metadata(m.generate());
Alex Rayb0be1032013-05-28 15:52:47 -0700300}
301
Alex Raybcaf7882013-02-28 16:04:35 -0800302int Camera::configureStreams(camera3_stream_configuration_t *stream_config)
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800303{
Alex Raybcaf7882013-02-28 16:04:35 -0800304 camera3_stream_t *astream;
305 Stream **newStreams = NULL;
306
Alex Rayea803822013-10-14 15:56:43 -0700307 ATRACE_CALL();
Alex Raybcaf7882013-02-28 16:04:35 -0800308 ALOGV("%s:%d: stream_config=%p", __func__, mId, stream_config);
309
310 if (stream_config == NULL) {
311 ALOGE("%s:%d: NULL stream configuration array", __func__, mId);
312 return -EINVAL;
313 }
314 if (stream_config->num_streams == 0) {
315 ALOGE("%s:%d: Empty stream configuration array", __func__, mId);
316 return -EINVAL;
317 }
318
319 // Create new stream array
320 newStreams = new Stream*[stream_config->num_streams];
321 ALOGV("%s:%d: Number of Streams: %d", __func__, mId,
322 stream_config->num_streams);
323
324 pthread_mutex_lock(&mMutex);
325
326 // Mark all current streams unused for now
327 for (int i = 0; i < mNumStreams; i++)
328 mStreams[i]->mReuse = false;
329 // Fill new stream array with reused streams and new streams
Alex Rayc6bf2f22013-05-28 15:52:04 -0700330 for (unsigned int i = 0; i < stream_config->num_streams; i++) {
Alex Raybcaf7882013-02-28 16:04:35 -0800331 astream = stream_config->streams[i];
Alex Ray2b286da2013-05-29 15:08:29 -0700332 if (astream->max_buffers > 0) {
333 ALOGV("%s:%d: Reusing stream %d", __func__, mId, i);
Alex Raybcaf7882013-02-28 16:04:35 -0800334 newStreams[i] = reuseStream(astream);
Alex Ray2b286da2013-05-29 15:08:29 -0700335 } else {
336 ALOGV("%s:%d: Creating new stream %d", __func__, mId, i);
Alex Raybcaf7882013-02-28 16:04:35 -0800337 newStreams[i] = new Stream(mId, astream);
Alex Ray2b286da2013-05-29 15:08:29 -0700338 }
Alex Raybcaf7882013-02-28 16:04:35 -0800339
340 if (newStreams[i] == NULL) {
341 ALOGE("%s:%d: Error processing stream %d", __func__, mId, i);
342 goto err_out;
343 }
344 astream->priv = newStreams[i];
345 }
346
347 // Verify the set of streams in aggregate
348 if (!isValidStreamSet(newStreams, stream_config->num_streams)) {
349 ALOGE("%s:%d: Invalid stream set", __func__, mId);
350 goto err_out;
351 }
352
353 // Set up all streams (calculate usage/max_buffers for each)
354 setupStreams(newStreams, stream_config->num_streams);
355
356 // Destroy all old streams and replace stream array with new one
357 destroyStreams(mStreams, mNumStreams);
358 mStreams = newStreams;
359 mNumStreams = stream_config->num_streams;
360
Alex Raybfcbd952013-03-20 13:20:02 -0700361 // Clear out last seen settings metadata
362 setSettings(NULL);
363
Alex Raybcaf7882013-02-28 16:04:35 -0800364 pthread_mutex_unlock(&mMutex);
Alex Raya0ed4be2013-02-25 15:02:16 -0800365 return 0;
Alex Raybcaf7882013-02-28 16:04:35 -0800366
367err_out:
368 // Clean up temporary streams, preserve existing mStreams/mNumStreams
369 destroyStreams(newStreams, stream_config->num_streams);
370 pthread_mutex_unlock(&mMutex);
371 return -EINVAL;
372}
373
374void Camera::destroyStreams(Stream **streams, int count)
375{
376 if (streams == NULL)
377 return;
378 for (int i = 0; i < count; i++) {
379 // Only destroy streams that weren't reused
380 if (streams[i] != NULL && !streams[i]->mReuse)
381 delete streams[i];
382 }
383 delete [] streams;
384}
385
386Stream *Camera::reuseStream(camera3_stream_t *astream)
387{
388 Stream *priv = reinterpret_cast<Stream*>(astream->priv);
389 // Verify the re-used stream's parameters match
390 if (!priv->isValidReuseStream(mId, astream)) {
391 ALOGE("%s:%d: Mismatched parameter in reused stream", __func__, mId);
392 return NULL;
393 }
394 // Mark stream to be reused
395 priv->mReuse = true;
396 return priv;
397}
398
399bool Camera::isValidStreamSet(Stream **streams, int count)
400{
401 int inputs = 0;
402 int outputs = 0;
403
404 if (streams == NULL) {
405 ALOGE("%s:%d: NULL stream configuration streams", __func__, mId);
406 return false;
407 }
408 if (count == 0) {
409 ALOGE("%s:%d: Zero count stream configuration streams", __func__, mId);
410 return false;
411 }
412 // Validate there is at most one input stream and at least one output stream
413 for (int i = 0; i < count; i++) {
414 // A stream may be both input and output (bidirectional)
415 if (streams[i]->isInputType())
416 inputs++;
417 if (streams[i]->isOutputType())
418 outputs++;
419 }
Alex Ray768216e2013-07-02 16:56:14 -0700420 ALOGV("%s:%d: Configuring %d output streams and %d input streams",
421 __func__, mId, outputs, inputs);
Alex Raybcaf7882013-02-28 16:04:35 -0800422 if (outputs < 1) {
423 ALOGE("%s:%d: Stream config must have >= 1 output", __func__, mId);
424 return false;
425 }
426 if (inputs > 1) {
427 ALOGE("%s:%d: Stream config must have <= 1 input", __func__, mId);
428 return false;
429 }
430 // TODO: check for correct number of Bayer/YUV/JPEG/Encoder streams
431 return true;
432}
433
434void Camera::setupStreams(Stream **streams, int count)
435{
436 /*
437 * This is where the HAL has to decide internally how to handle all of the
438 * streams, and then produce usage and max_buffer values for each stream.
439 * Note, the stream array has been checked before this point for ALL invalid
440 * conditions, so it must find a successful configuration for this stream
441 * array. The HAL may not return an error from this point.
442 *
443 * In this demo HAL, we just set all streams to be the same dummy values;
444 * real implementations will want to avoid USAGE_SW_{READ|WRITE}_OFTEN.
445 */
446 for (int i = 0; i < count; i++) {
447 uint32_t usage = 0;
448
449 if (streams[i]->isOutputType())
450 usage |= GRALLOC_USAGE_SW_WRITE_OFTEN |
451 GRALLOC_USAGE_HW_CAMERA_WRITE;
452 if (streams[i]->isInputType())
453 usage |= GRALLOC_USAGE_SW_READ_OFTEN |
454 GRALLOC_USAGE_HW_CAMERA_READ;
455
456 streams[i]->setUsage(usage);
457 streams[i]->setMaxBuffers(1);
458 }
Alex Raya0ed4be2013-02-25 15:02:16 -0800459}
460
461int Camera::registerStreamBuffers(const camera3_stream_buffer_set_t *buf_set)
462{
463 ALOGV("%s:%d: buffer_set=%p", __func__, mId, buf_set);
Alex Ray8a8f86b2013-03-01 01:32:21 -0800464 if (buf_set == NULL) {
465 ALOGE("%s:%d: NULL buffer set", __func__, mId);
466 return -EINVAL;
467 }
468 if (buf_set->stream == NULL) {
469 ALOGE("%s:%d: NULL stream handle", __func__, mId);
470 return -EINVAL;
471 }
472 Stream *stream = reinterpret_cast<Stream*>(buf_set->stream->priv);
473 return stream->registerBuffers(buf_set);
Alex Raya0ed4be2013-02-25 15:02:16 -0800474}
475
476const camera_metadata_t* Camera::constructDefaultRequestSettings(int type)
477{
478 ALOGV("%s:%d: type=%d", __func__, mId, type);
Alex Ray89a82662013-05-28 20:32:48 -0700479
480 if (type < 1 || type >= CAMERA3_TEMPLATE_COUNT) {
481 ALOGE("%s:%d: Invalid template request type: %d", __func__, mId, type);
482 return NULL;
483 }
484 return mTemplates[type]->generate();
Alex Raya0ed4be2013-02-25 15:02:16 -0800485}
486
487int Camera::processCaptureRequest(camera3_capture_request_t *request)
488{
Alex Ray083315c2013-04-26 19:32:29 -0700489 camera3_capture_result result;
490
Alex Raya0ed4be2013-02-25 15:02:16 -0800491 ALOGV("%s:%d: request=%p", __func__, mId, request);
Alex Rayea803822013-10-14 15:56:43 -0700492 ATRACE_CALL();
Alex Raya0ed4be2013-02-25 15:02:16 -0800493
494 if (request == NULL) {
495 ALOGE("%s:%d: NULL request recieved", __func__, mId);
Alex Raya0ed4be2013-02-25 15:02:16 -0800496 return -EINVAL;
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800497 }
498
Alex Raybfcbd952013-03-20 13:20:02 -0700499 ALOGV("%s:%d: Request Frame:%d Settings:%p", __func__, mId,
500 request->frame_number, request->settings);
501
502 // NULL indicates use last settings
503 if (request->settings == NULL) {
504 if (mSettings == NULL) {
505 ALOGE("%s:%d: NULL settings without previous set Frame:%d Req:%p",
506 __func__, mId, request->frame_number, request);
507 return -EINVAL;
508 }
509 } else {
510 setSettings(request->settings);
511 }
512
Alex Ray11bbeef2013-04-26 14:47:08 -0700513 if (request->input_buffer != NULL) {
514 ALOGV("%s:%d: Reprocessing input buffer %p", __func__, mId,
515 request->input_buffer);
516
517 if (!isValidReprocessSettings(request->settings)) {
518 ALOGE("%s:%d: Invalid settings for reprocess request: %p",
519 __func__, mId, request->settings);
520 return -EINVAL;
521 }
522 } else {
523 ALOGV("%s:%d: Capturing new frame.", __func__, mId);
524
525 if (!isValidCaptureSettings(request->settings)) {
526 ALOGE("%s:%d: Invalid settings for capture request: %p",
527 __func__, mId, request->settings);
528 return -EINVAL;
529 }
530 }
531
Alex Ray083315c2013-04-26 19:32:29 -0700532 if (request->num_output_buffers <= 0) {
533 ALOGE("%s:%d: Invalid number of output buffers: %d", __func__, mId,
534 request->num_output_buffers);
535 return -EINVAL;
536 }
537 result.num_output_buffers = request->num_output_buffers;
538 result.output_buffers = new camera3_stream_buffer_t[result.num_output_buffers];
539 for (unsigned int i = 0; i < request->num_output_buffers; i++) {
540 int res = processCaptureBuffer(&request->output_buffers[i],
541 const_cast<camera3_stream_buffer_t*>(&result.output_buffers[i]));
542 if (res)
543 goto err_out;
544 }
545
546 result.frame_number = request->frame_number;
547 // TODO: return actual captured/reprocessed settings
548 result.result = request->settings;
549 // TODO: asynchronously return results
Alex Ray764e4422013-06-04 12:38:07 -0700550 notifyShutter(request->frame_number, 0);
Alex Ray083315c2013-04-26 19:32:29 -0700551 mCallbackOps->process_capture_result(mCallbackOps, &result);
552
Alex Raya0ed4be2013-02-25 15:02:16 -0800553 return 0;
Alex Ray083315c2013-04-26 19:32:29 -0700554
555err_out:
556 delete [] result.output_buffers;
557 // TODO: this should probably be a total device failure; transient for now
558 return -EINVAL;
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800559}
560
Alex Raybfcbd952013-03-20 13:20:02 -0700561void Camera::setSettings(const camera_metadata_t *new_settings)
562{
563 if (mSettings != NULL) {
564 free_camera_metadata(mSettings);
565 mSettings = NULL;
566 }
567
568 if (new_settings != NULL)
569 mSettings = clone_camera_metadata(new_settings);
570}
571
Alex Rayc6bf2f22013-05-28 15:52:04 -0700572bool Camera::isValidCaptureSettings(const camera_metadata_t* /*settings*/)
Alex Ray11bbeef2013-04-26 14:47:08 -0700573{
574 // TODO: reject settings that cannot be captured
575 return true;
576}
577
Alex Rayc6bf2f22013-05-28 15:52:04 -0700578bool Camera::isValidReprocessSettings(const camera_metadata_t* /*settings*/)
Alex Ray11bbeef2013-04-26 14:47:08 -0700579{
580 // TODO: reject settings that cannot be reprocessed
581 // input buffers unimplemented, use this to reject reprocessing requests
582 ALOGE("%s:%d: Input buffer reprocessing not implemented", __func__, mId);
583 return false;
584}
585
Alex Ray083315c2013-04-26 19:32:29 -0700586int Camera::processCaptureBuffer(const camera3_stream_buffer_t *in,
587 camera3_stream_buffer_t *out)
588{
Alex Ray77ecfd72013-05-30 00:19:04 -0700589 if (in->acquire_fence != -1) {
590 int res = sync_wait(in->acquire_fence, CAMERA_SYNC_TIMEOUT);
591 if (res == -ETIME) {
592 ALOGE("%s:%d: Timeout waiting on buffer acquire fence",
593 __func__, mId);
594 return res;
595 } else if (res) {
596 ALOGE("%s:%d: Error waiting on buffer acquire fence: %s(%d)",
597 __func__, mId, strerror(-res), res);
598 return res;
599 }
Alex Ray083315c2013-04-26 19:32:29 -0700600 }
601
602 out->stream = in->stream;
603 out->buffer = in->buffer;
604 out->status = CAMERA3_BUFFER_STATUS_OK;
605 // TODO: use driver-backed release fences
606 out->acquire_fence = -1;
607 out->release_fence = -1;
608
609 // TODO: lock and software-paint buffer
610 return 0;
611}
612
Alex Ray764e4422013-06-04 12:38:07 -0700613void Camera::notifyShutter(uint32_t frame_number, uint64_t timestamp)
614{
615 int res;
616 struct timespec ts;
617
618 // If timestamp is 0, get timestamp from right now instead
619 if (timestamp == 0) {
620 ALOGW("%s:%d: No timestamp provided, using CLOCK_BOOTTIME",
621 __func__, mId);
622 res = clock_gettime(CLOCK_BOOTTIME, &ts);
623 if (res == 0) {
624 timestamp = ts.tv_sec * 1000000000ULL + ts.tv_nsec;
625 } else {
626 ALOGE("%s:%d: No timestamp and failed to get CLOCK_BOOTTIME %s(%d)",
627 __func__, mId, strerror(errno), errno);
628 }
629 }
630 camera3_notify_msg_t m;
631 memset(&m, 0, sizeof(m));
632 m.type = CAMERA3_MSG_SHUTTER;
633 m.message.shutter.frame_number = frame_number;
634 m.message.shutter.timestamp = timestamp;
635 mCallbackOps->notify(mCallbackOps, &m);
636}
637
Alex Raya0ed4be2013-02-25 15:02:16 -0800638void Camera::getMetadataVendorTagOps(vendor_tag_query_ops_t *ops)
639{
640 ALOGV("%s:%d: ops=%p", __func__, mId, ops);
641 // TODO: return vendor tag ops
642}
643
644void Camera::dump(int fd)
645{
Alex Rayaf3a4612013-04-29 14:16:10 -0700646 ALOGV("%s:%d: Dumping to fd %d", __func__, mId, fd);
Alex Raya0ed4be2013-02-25 15:02:16 -0800647 // TODO: dprintf all relevant state to fd
648}
649
650extern "C" {
651// Get handle to camera from device priv data
652static Camera *camdev_to_camera(const camera3_device_t *dev)
653{
654 return reinterpret_cast<Camera*>(dev->priv);
655}
656
657static int initialize(const camera3_device_t *dev,
658 const camera3_callback_ops_t *callback_ops)
659{
660 return camdev_to_camera(dev)->initialize(callback_ops);
661}
662
663static int configure_streams(const camera3_device_t *dev,
664 camera3_stream_configuration_t *stream_list)
665{
666 return camdev_to_camera(dev)->configureStreams(stream_list);
667}
668
669static int register_stream_buffers(const camera3_device_t *dev,
670 const camera3_stream_buffer_set_t *buffer_set)
671{
672 return camdev_to_camera(dev)->registerStreamBuffers(buffer_set);
673}
674
675static const camera_metadata_t *construct_default_request_settings(
676 const camera3_device_t *dev, int type)
677{
678 return camdev_to_camera(dev)->constructDefaultRequestSettings(type);
679}
680
681static int process_capture_request(const camera3_device_t *dev,
682 camera3_capture_request_t *request)
683{
684 return camdev_to_camera(dev)->processCaptureRequest(request);
685}
686
687static void get_metadata_vendor_tag_ops(const camera3_device_t *dev,
688 vendor_tag_query_ops_t *ops)
689{
690 camdev_to_camera(dev)->getMetadataVendorTagOps(ops);
691}
692
693static void dump(const camera3_device_t *dev, int fd)
694{
695 camdev_to_camera(dev)->dump(fd);
696}
697} // extern "C"
698
699const camera3_device_ops_t Camera::sOps = {
700 .initialize = default_camera_hal::initialize,
701 .configure_streams = default_camera_hal::configure_streams,
702 .register_stream_buffers = default_camera_hal::register_stream_buffers,
703 .construct_default_request_settings =
704 default_camera_hal::construct_default_request_settings,
705 .process_capture_request = default_camera_hal::process_capture_request,
706 .get_metadata_vendor_tag_ops =
707 default_camera_hal::get_metadata_vendor_tag_ops,
708 .dump = default_camera_hal::dump
709};
710
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800711} // namespace default_camera_hal