blob: b3ae4fcdb3a152d94a7e8b148870dc7489ea3d7e [file] [log] [blame]
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001/*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "V4L2Camera.h"
18
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070019#include <fcntl.h>
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070020#include <linux/videodev2.h>
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070021#include <sys/types.h>
22#include <sys/stat.h>
23
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070024#include <cstdlib>
25
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070026#include <camera/CameraMetadata.h>
27#include <hardware/camera3.h>
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070028#include <nativehelper/ScopedFd.h>
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070029
30#include "Common.h"
31
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070032#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
33
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070034namespace v4l2_camera_hal {
35
Ari Hausman-Cohendde80172016-07-01 16:20:36 -070036// Helper function for managing metadata.
37static std::vector<int32_t> getMetadataKeys(
38 const camera_metadata_t* metadata) {
39 std::vector<int32_t> keys;
40 size_t num_entries = get_camera_metadata_entry_count(metadata);
41 for (size_t i = 0; i < num_entries; ++i) {
42 camera_metadata_ro_entry_t entry;
43 get_camera_metadata_ro_entry(metadata, i, &entry);
44 keys.push_back(entry.tag);
45 }
46 return keys;
47}
48
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070049V4L2Camera::V4L2Camera(int id, std::string path)
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070050 : default_camera_hal::Camera(id),
51 mDevicePath(std::move(path)),
52 mTemplatesInitialized(false),
53 mCharacteristicsInitialized(false) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070054 HAL_LOG_ENTER();
55}
56
57V4L2Camera::~V4L2Camera() {
58 HAL_LOG_ENTER();
59}
60
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070061int V4L2Camera::connect() {
62 HAL_LOG_ENTER();
63
64 if (mDeviceFd.get() >= 0) {
65 HAL_LOGE("Camera device %s is opened. Close it first", mDevicePath.c_str());
66 return -EIO;
67 }
68
69 int fd = TEMP_FAILURE_RETRY(open(mDevicePath.c_str(), O_RDWR));
70 if (fd < 0) {
71 HAL_LOGE("failed to open %s (%s)", mDevicePath.c_str(), strerror(errno));
72 return -errno;
73 }
74 mDeviceFd.reset(fd);
75
76 // TODO(b/29185945): confirm this is a supported device.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070077 // This is checked by the HAL, but the device at mDevicePath may
78 // not be the same one that was there when the HAL was loaded.
79 // (Alternatively, better hotplugging support may make this unecessary
80 // by disabling cameras that get disconnected and checking newly connected
81 // cameras, so connect() is never called on an unsupported camera)
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070082
83 // TODO(b/29158098): Inform service of any flashes that are no longer available
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070084 // because this camera is in use.
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070085 return 0;
86}
87
88void V4L2Camera::disconnect() {
89 HAL_LOG_ENTER();
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070090 // TODO(b/29158098): Inform service of any flashes that are available again
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070091 // because this camera is no longer in use.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070092
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070093 mDeviceFd.reset();
94}
95
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070096int V4L2Camera::initStaticInfo(camera_metadata_t** out) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070097 HAL_LOG_ENTER();
98
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070099 android::status_t res;
100 // Device characteristics need to be queried prior
101 // to static info setup.
102 if (!mCharacteristicsInitialized) {
103 res = initCharacteristics();
104 if (res) {
105 return res;
106 }
107 }
108
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700109 android::CameraMetadata info;
Ari Hausman-Cohen63f69822016-06-10 11:40:35 -0700110
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700111 // Static metadata characteristics from /system/media/camera/docs/docs.html.
112
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700113 /* android.colorCorrection. */
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700114
115 // No easy way to turn chromatic aberration correction OFF in v4l2,
116 // though this may be supportable via a collection of other user controls.
117 uint8_t avail_aberration_modes[] = {
118 ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST,
119 ANDROID_COLOR_CORRECTION_ABERRATION_MODE_HIGH_QUALITY};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700120 res = info.update(ANDROID_COLOR_CORRECTION_AVAILABLE_ABERRATION_MODES,
121 avail_aberration_modes, ARRAY_SIZE(avail_aberration_modes));
122 if (res != android::OK) {
123 return res;
124 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700125
126 /* android.control. */
127
128 /* 3As */
129
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700130 res = info.update(ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES,
131 mAeAntibandingModes.data(), mAeAntibandingModes.size());
132 if (res != android::OK) {
133 return res;
134 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700135
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700136 res = info.update(ANDROID_CONTROL_AE_AVAILABLE_MODES,
137 mAeModes.data(), mAeModes.size());
138 if (res != android::OK) {
139 return res;
140 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700141
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700142 // Flatten mFpsRanges.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700143 res = info.update(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES,
144 mFpsRanges.data(), mFpsRanges.total_num_elements());
145 if (res != android::OK) {
146 return res;
147 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700148
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700149 res = info.update(ANDROID_CONTROL_AE_COMPENSATION_RANGE,
150 mAeCompensationRange.data(), mAeCompensationRange.size());
151 if (res != android::OK) {
152 return res;
153 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700154
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700155 res = info.update(ANDROID_CONTROL_AE_COMPENSATION_STEP,
156 &mAeCompensationStep, 1);
157 if (res != android::OK) {
158 return res;
159 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700160
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700161 res = info.update(ANDROID_CONTROL_AF_AVAILABLE_MODES,
162 mAfModes.data(), mAfModes.size());
163 if (res != android::OK) {
164 return res;
165 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700166
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700167 res = info.update(ANDROID_CONTROL_AWB_AVAILABLE_MODES,
168 mAwbModes.data(), mAwbModes.size());
169 if (res != android::OK) {
170 return res;
171 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700172
173 // Couldn't find any V4L2 support for regions, though maybe it's out there.
174 int32_t max_regions[] = {/*AE*/ 0,/*AWB*/ 0,/*AF*/ 0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700175 res = info.update(ANDROID_CONTROL_MAX_REGIONS,
176 max_regions, ARRAY_SIZE(max_regions));
177 if (res != android::OK) {
178 return res;
179 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700180
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700181 res = info.update(ANDROID_CONTROL_AE_LOCK_AVAILABLE,
182 &mAeLockAvailable, 1);
183 if (res != android::OK) {
184 return res;
185 }
186 res = info.update(ANDROID_CONTROL_AWB_LOCK_AVAILABLE,
187 &mAwbLockAvailable, 1);
188 if (res != android::OK) {
189 return res;
190 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700191
192 /* Scene modes. */
193
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700194 res = info.update(ANDROID_CONTROL_AVAILABLE_SCENE_MODES,
195 mSceneModes.data(), mSceneModes.size());
196 if (res != android::OK) {
197 return res;
198 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700199
200 // A 3-tuple of AE, AWB, AF overrides for each scene mode.
201 // Ignored for DISABLED, FACE_PRIORITY and FACE_PRIORITY_LOW_LIGHT.
202 uint8_t scene_mode_overrides[] = {
203 /*SCENE_MODE_DISABLED*/ /*AE*/0, /*AW*/0, /*AF*/0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700204 res = info.update(ANDROID_CONTROL_SCENE_MODE_OVERRIDES,
205 scene_mode_overrides, ARRAY_SIZE(scene_mode_overrides));
206 if (res != android::OK) {
207 return res;
208 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700209
210 /* Top level 3A/Scenes switch. */
211
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700212 res = info.update(ANDROID_CONTROL_AVAILABLE_MODES,
213 mControlModes.data(), mControlModes.size());
214 if (res != android::OK) {
215 return res;
216 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700217
218 /* Other android.control configuration. */
219
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700220 res = info.update(ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES,
221 mVideoStabilizationModes.data(),
222 mVideoStabilizationModes.size());
223 if (res != android::OK) {
224 return res;
225 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700226
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700227 res = info.update(ANDROID_CONTROL_AVAILABLE_EFFECTS,
228 mEffects.data(), mEffects.size());
229 if (res != android::OK) {
230 return res;
231 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700232
233 // AVAILABLE_HIGH_SPEED_VIDEO_CONFIGURATIONS only necessary
234 // for devices supporting CONSTRAINED_HIGH_SPEED_VIDEO,
235 // which this HAL doesn't support.
236
237 // POST_RAW_SENSITIVITY_BOOST_RANGE only necessary
238 // for devices supporting RAW format outputs.
239
240 /* android.edge. */
241
242 // Not sure if V4L2 does or doesn't do this, but HAL documentation says
243 // all devices must support FAST, and FAST can be equivalent to OFF, so
244 // either way it's fine to list.
245 uint8_t avail_edge_modes[] = {
246 ANDROID_EDGE_MODE_FAST};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700247 res = info.update(ANDROID_EDGE_AVAILABLE_EDGE_MODES,
248 avail_edge_modes, ARRAY_SIZE(avail_edge_modes));
249 if (res != android::OK) {
250 return res;
251 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700252
253 /* android.flash. */
254
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700255 res = info.update(ANDROID_FLASH_INFO_AVAILABLE,
256 &mFlashAvailable, 1);
257 if (res != android::OK) {
258 return res;
259 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700260
261 // info.chargeDuration, color.Temperature, maxEnergy marked FUTURE.
262
263 /* android.hotPixel. */
264
265 // No known V4L2 hot pixel correction. But it might be happening,
266 // so we report FAST/HIGH_QUALITY.
267 uint8_t avail_hot_pixel_modes[] = {
268 ANDROID_HOT_PIXEL_MODE_FAST,
269 ANDROID_HOT_PIXEL_MODE_HIGH_QUALITY};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700270 res = info.update(ANDROID_HOT_PIXEL_AVAILABLE_HOT_PIXEL_MODES,
271 avail_hot_pixel_modes, ARRAY_SIZE(avail_hot_pixel_modes));
272 if (res != android::OK) {
273 return res;
274 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700275
276 /* android.jpeg. */
277
278 // For now, no thumbnails available (only [0,0], the "no thumbnail" size).
279 // TODO(b/29580107): Could end up with a mismatch between request & result,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700280 // since V4L2 doesn't actually allow for thumbnail size control.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700281 int32_t thumbnail_sizes[] = {
282 0, 0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700283 res = info.update(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES,
284 thumbnail_sizes, ARRAY_SIZE(thumbnail_sizes));
285 if (res != android::OK) {
286 return res;
287 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700288
289 // V4L2 doesn't support querying this, so we generously assume up to 3 MB.
290 int32_t max_jpeg_size = 3000000;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700291 res = info.update(ANDROID_JPEG_MAX_SIZE,
292 &max_jpeg_size, 1);
293 if (res != android::OK) {
294 return res;
295 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700296
297 /* android.lens. */
298
299 /* Misc. lens control. */
300
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700301 res = info.update(ANDROID_LENS_INFO_AVAILABLE_APERTURES,
302 &mAperture, 1);
303 if (res != android::OK) {
304 return res;
305 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700306
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700307 res = info.update(ANDROID_LENS_INFO_AVAILABLE_FILTER_DENSITIES,
308 &mFilterDensity, 1);
309 if (res != android::OK) {
310 return res;
311 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700312
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700313 res = info.update(ANDROID_LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION,
314 mOpticalStabilizationModes.data(),
315 mOpticalStabilizationModes.size());
316 if (res != android::OK) {
317 return res;
318 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700319
320 // No known V4L2 shading map info.
321 int32_t shading_map_size[] = {1, 1};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700322 res = info.update(ANDROID_LENS_INFO_SHADING_MAP_SIZE,
323 shading_map_size, ARRAY_SIZE(shading_map_size));
324 if (res != android::OK) {
325 return res;
326 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700327
328 // All V4L2 devices are considered to be external facing.
329 uint8_t facing = ANDROID_LENS_FACING_EXTERNAL;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700330 res = info.update(ANDROID_LENS_FACING, &facing, 1);
331 if (res != android::OK) {
332 return res;
333 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700334
335 /* Zoom/Focus. */
336
337 // No way to actually get the focal length in V4L2, but it's a required key,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700338 // so we just fake it.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700339 res = info.update(ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS,
340 &mFocalLength, 1);
341 if (res != android::OK) {
342 return res;
343 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700344
345 // V4L2 focal units do not correspond to a particular physical unit.
346 uint8_t focus_calibration =
347 ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION_UNCALIBRATED;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700348 res = info.update(ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION,
349 &focus_calibration, 1);
350 if (res != android::OK) {
351 return res;
352 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700353
354 // info.hyperfocalDistance not required for UNCALIBRATED.
355
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700356 res = info.update(ANDROID_LENS_INFO_MINIMUM_FOCUS_DISTANCE,
357 &mFocusDistance, 1);
358 if (res != android::OK) {
359 return res;
360 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700361
362 /* Depth. */
363
364 // DEPTH capability not supported by this HAL. Not implemented:
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700365 // poseRotation
366 // poseTranslation
367 // intrinsicCalibration
368 // radialDistortion
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700369
370 /* anroid.noise. */
371
372 // Unable to control noise reduction in V4L2 devices,
373 // but FAST is allowed to be the same as OFF.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700374 uint8_t avail_noise_reduction_modes[] = {ANDROID_NOISE_REDUCTION_MODE_FAST};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700375 res = info.update(ANDROID_NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES,
376 avail_noise_reduction_modes,
377 ARRAY_SIZE(avail_noise_reduction_modes));
378 if (res != android::OK) {
379 return res;
380 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700381
382 /* android.request. */
383
384 // Resources may be an issue, so just using minimum allowable
385 // for LIMITED devices.
386 int32_t max_num_output_streams[] = {
387 /*Raw*/0, /*YUV*/2, /*JPEG*/1};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700388 res = info.update(ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS,
389 max_num_output_streams, ARRAY_SIZE(max_num_output_streams));
390 if (res != android::OK) {
391 return res;
392 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700393
394 // Reprocessing not supported, so no maxNumInputStreams.
395
396 // No way to know for V4L2, so fake with max allowable latency.
397 // Doesn't mean much without per-frame controls.
398 uint8_t pipeline_max_depth = 4;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700399 res = info.update(ANDROID_REQUEST_PIPELINE_MAX_DEPTH,
400 &pipeline_max_depth, 1);
401 if (res != android::OK) {
402 return res;
403 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700404
405 // Partial results not supported; partialResultCount defaults to 1.
406
407 // Available capabilities & keys queried at very end of this method.
408
409 /* android.scaler. */
410
411 /* Cropping. */
412
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700413 res = info.update(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
414 &mMaxZoom, 1);
415 if (res != android::OK) {
416 return res;
417 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700418
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700419 res = info.update(ANDROID_SCALER_CROPPING_TYPE, &mCropType, 1);
420 if (res != android::OK) {
421 return res;
422 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700423
424 /* Streams. */
425
426 // availableInputOutputFormatsMap only required for reprocessing capability.
427
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700428 // Flatten mStreamConfigs.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700429 res = info.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
430 mStreamConfigs.data(),
431 mStreamConfigs.total_num_elements());
432 if (res != android::OK) {
433 return res;
434 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700435
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700436 // Flatten mMinFrameDurations.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700437 res = info.update(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS,
438 mMinFrameDurations.data(),
439 mMinFrameDurations.total_num_elements());
440 if (res != android::OK) {
441 return res;
442 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700443
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700444 // Flatten mStallDurations.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700445 res = info.update(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS,
446 mStallDurations.data(),
447 mStallDurations.total_num_elements());
448 if (res != android::OK) {
449 return res;
450 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700451
452 /* android.sensor. */
453
454 /* Sizes. */
455
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700456 res = info.update(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE,
457 mPixelArraySize.data(), mPixelArraySize.size());
458 if (res != android::OK) {
459 return res;
460 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700461 // No V4L2 way to differentiate active vs. inactive parts of the rectangle.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700462 res = info.update(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE,
463 mPixelArraySize.data(), mPixelArraySize.size());
464 if (res != android::OK) {
465 return res;
466 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700467
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700468 res = info.update(ANDROID_SENSOR_INFO_PHYSICAL_SIZE,
469 mPhysicalSize.data(), mPhysicalSize.size());
470 if (res != android::OK) {
471 return res;
472 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700473
474 /* Misc sensor information. */
475
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700476 res = info.update(ANDROID_SENSOR_INFO_MAX_FRAME_DURATION,
477 &mMaxFrameDuration, 1);
478 if (res != android::OK) {
479 return res;
480 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700481
482 // HAL uses BOOTTIME timestamps.
483 // TODO(b/29457051): make sure timestamps are consistent throughout the HAL.
484 uint8_t timestamp_source = ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700485 res = info.update(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE,
486 &timestamp_source, 1);
487 if (res != android::OK) {
488 return res;
489 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700490
491 // As in initDeviceInfo, no way to actually get orientation.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700492 res = info.update(ANDROID_SENSOR_ORIENTATION, &mOrientation, 1);
493 if (res != android::OK) {
494 return res;
495 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700496
497 // availableTestPatternModes just defaults to OFF, which is fine.
498
499 // info.exposureTimeRange, info.sensitivityRange:
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700500 // exposure/sensitivity manual control not supported.
501 // Could query V4L2_CID_ISO_SENSITIVITY to support sensitivity if desired.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700502
503 // info.whiteLevel, info.lensShadingApplied,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700504 // info.preCorrectionPixelArraySize, referenceIlluminant1/2,
505 // calibrationTransform1/2, colorTransform1/2, forwardMatrix1/2,
506 // blackLevelPattern, profileHueSatMapDimensions
507 // all only necessary for RAW.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700508
509 // baseGainFactor marked FUTURE.
510
511 // maxAnalogSensitivity optional for LIMITED device.
512
513 // opticalBlackRegions: No known way to get in V4L2, but not necessary.
514
515 // opaqueRawSize not necessary since RAW_OPAQUE format not supported.
516
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700517 /* android.shading */
518
519 // No known V4L2 lens shading. But it might be happening,
520 // so we report FAST/HIGH_QUALITY.
521 uint8_t avail_shading_modes[] = {
522 ANDROID_SHADING_MODE_FAST,
523 ANDROID_SHADING_MODE_HIGH_QUALITY};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700524 res = info.update(ANDROID_SHADING_AVAILABLE_MODES,
525 avail_shading_modes, ARRAY_SIZE(avail_shading_modes));
526 if (res != android::OK) {
527 return res;
528 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700529
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700530 /* android.statistics */
531
532 // Face detection not supported.
533 uint8_t avail_face_detect_modes[] = {
534 ANDROID_STATISTICS_FACE_DETECT_MODE_OFF};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700535 res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES,
536 avail_face_detect_modes,
537 ARRAY_SIZE(avail_face_detect_modes));
538 if (res != android::OK) {
539 return res;
540 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700541
542 int32_t max_face_count = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700543 res = info.update(ANDROID_STATISTICS_INFO_MAX_FACE_COUNT,
544 &max_face_count, 1);
545 if (res != android::OK) {
546 return res;
547 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700548
549 // info.histogramBucketCount, info.maxHistogramCount,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700550 // info.maxSharpnessMapValue, info.sharpnessMapSizemarked FUTURE.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700551
552 // ON only needs to be supported for RAW capable devices.
553 uint8_t avail_hot_pixel_map_modes[] = {
554 ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700555 res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_HOT_PIXEL_MAP_MODES,
556 avail_hot_pixel_map_modes,
557 ARRAY_SIZE(avail_hot_pixel_map_modes));
558 if (res != android::OK) {
559 return res;
560 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700561
562 // ON only needs to be supported for RAW capable devices.
563 uint8_t avail_lens_shading_map_modes[] = {
564 ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700565 res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES,
566 avail_lens_shading_map_modes,
567 ARRAY_SIZE(avail_lens_shading_map_modes));
568 if (res != android::OK) {
569 return res;
570 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700571
572 /* android.tonemap. */
573
574 // tonemapping only required for MANUAL_POST_PROCESSING capability.
575
576 /* android.led. */
577
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700578 // May or may not have LEDs available.
579 if (!mLeds.empty()) {
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700580 res = info.update(ANDROID_LED_AVAILABLE_LEDS, mLeds.data(), mLeds.size());
581 if (res != android::OK) {
582 return res;
583 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700584 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700585
586 /* android.sync. */
587
588 // "LIMITED devices are strongly encouraged to use a non-negative value.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700589 // If UNKNOWN is used here then app developers do not have a way to know
590 // when sensor settings have been applied." - Unfortunately, V4L2 doesn't
591 // really help here either. Could even be that adjusting settings mid-stream
592 // blocks in V4L2, and should be avoided.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700593 int32_t max_latency = ANDROID_SYNC_MAX_LATENCY_UNKNOWN;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700594 res = info.update(ANDROID_SYNC_MAX_LATENCY,
595 &max_latency, 1);
596 if (res != android::OK) {
597 return res;
598 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700599
600 /* android.reprocess. */
601
602 // REPROCESSING not supported by this HAL.
603
604 /* android.depth. */
605
606 // DEPTH not supported by this HAL.
607
608 /* Capabilities and android.info. */
609
610 uint8_t hw_level = ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700611 res = info.update(ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL,
612 &hw_level, 1);
613 if (res != android::OK) {
614 return res;
615 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700616
617 uint8_t capabilities[] = {
618 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700619 res = info.update(ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
620 capabilities, ARRAY_SIZE(capabilities));
621 if (res != android::OK) {
622 return res;
623 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700624
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700625 // Scan a default request template for included request keys.
626 if (!mTemplatesInitialized) {
627 res = initTemplates();
628 if (res) {
629 return res;
630 }
631 }
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700632 const camera_metadata_t* preview_request = nullptr;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700633 // Search templates from the beginning for a supported one.
634 for (uint8_t template_id = 1; template_id < CAMERA3_TEMPLATE_COUNT;
635 ++template_id) {
636 preview_request = constructDefaultRequestSettings(template_id);
637 if (preview_request != nullptr) {
638 break;
639 }
640 }
641 if (preview_request == nullptr) {
642 HAL_LOGE("No valid templates, can't get request keys.");
643 return -ENODEV;
644 }
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700645 std::vector<int32_t> avail_request_keys = getMetadataKeys(preview_request);
646 res = info.update(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS,
647 avail_request_keys.data(), avail_request_keys.size());
648 if (res != android::OK) {
649 return res;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700650 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700651
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700652 // Result keys will be duplicated from the request, plus a few extras.
653 // TODO(b/29335262): additonal available result keys.
654 std::vector<int32_t> avail_result_keys(avail_request_keys);
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700655 res = info.update(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS,
656 avail_result_keys.data(), avail_result_keys.size());
657 if (res != android::OK) {
658 return res;
659 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700660
661 // Last thing, once all the available characteristics have been added.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700662 const camera_metadata_t* static_characteristics = info.getAndLock();
663 std::vector<int32_t> avail_characteristics_keys =
664 getMetadataKeys(static_characteristics);
665 res = info.unlock(static_characteristics);
666 if (res != android::OK) {
667 return res;
668 }
669 avail_characteristics_keys.push_back(
670 ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
671 res = info.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS,
672 avail_characteristics_keys.data(),
673 avail_characteristics_keys.size());
674 if (res != android::OK) {
675 return res;
676 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700677
678 *out = info.release();
679 return 0;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700680}
681
682void V4L2Camera::initDeviceInfo(camera_info_t* info) {
683 HAL_LOG_ENTER();
684
685 // For now, just constants.
686 info->facing = CAMERA_FACING_EXTERNAL;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700687 info->orientation = mOrientation;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700688 info->resource_cost = 100;
689 info->conflicting_devices = nullptr;
690 info->conflicting_devices_length = 0;
691}
692
693int V4L2Camera::initDevice() {
694 HAL_LOG_ENTER();
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700695 int res;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700696
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700697 // Templates should be set up if they haven't already been.
698 if (!mTemplatesInitialized) {
699 res = initTemplates();
700 if (res) {
701 return res;
702 }
703 }
704
705 return 0;
706}
707
708int V4L2Camera::initTemplates() {
709 HAL_LOG_ENTER();
710 int res;
711
712 // Device characteristics need to be queried prior
713 // to template setup.
714 if (!mCharacteristicsInitialized) {
715 res = initCharacteristics();
716 if (res) {
717 return res;
718 }
719 }
720
721 // Note: static metadata expects all templates/requests
722 // to provide values for all supported keys.
723
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700724 android::CameraMetadata base_metadata;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700725
726 // Start with defaults for all templates.
727
728 /* android.colorCorrection. */
729
730 uint8_t aberration_mode = ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700731 res = base_metadata.update(ANDROID_COLOR_CORRECTION_ABERRATION_MODE,
732 &aberration_mode, 1);
733 if (res != android::OK) {
734 return res;
735 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700736
737 uint8_t color_correction_mode = ANDROID_COLOR_CORRECTION_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700738 res = base_metadata.update(ANDROID_COLOR_CORRECTION_MODE,
739 &color_correction_mode, 1);
740 if (res != android::OK) {
741 return res;
742 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700743
744 // transform and gains are for the unsupported MANUAL_POST_PROCESSING only.
745
746 /* android.control. */
747
748 /* AE. */
749 uint8_t ae_antibanding_mode = ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700750 res = base_metadata.update(ANDROID_CONTROL_AE_ANTIBANDING_MODE,
751 &ae_antibanding_mode, 1);
752 if (res != android::OK) {
753 return res;
754 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700755
756 // Only matters if AE_MODE = OFF
757 int32_t ae_exposure_compensation = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700758 res = base_metadata.update(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION,
759 &ae_exposure_compensation, 1);
760 if (res != android::OK) {
761 return res;
762 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700763
764 uint8_t ae_lock = ANDROID_CONTROL_AE_LOCK_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700765 res = base_metadata.update(ANDROID_CONTROL_AE_LOCK, &ae_lock, 1);
766 if (res != android::OK) {
767 return res;
768 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700769
770 uint8_t ae_mode = ANDROID_CONTROL_AE_MODE_ON;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700771 res = base_metadata.update(ANDROID_CONTROL_AE_MODE, &ae_mode, 1);
772 if (res != android::OK) {
773 return res;
774 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700775
776 // AE regions not supported.
777
778 // FPS set per-template.
779
780 uint8_t ae_precapture_trigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700781 res = base_metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
782 &ae_precapture_trigger, 1);
783 if (res != android::OK) {
784 return res;
785 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700786
787 /* AF. */
788
789 // AF mode set per-template.
790
791 // AF regions not supported.
792
793 uint8_t af_trigger = ANDROID_CONTROL_AF_TRIGGER_IDLE;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700794 res = base_metadata.update(ANDROID_CONTROL_AF_TRIGGER, &af_trigger, 1);
795 if (res != android::OK) {
796 return res;
797 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700798
799 /* AWB. */
800
801 // Priority: auto > off > Whatever is available.
802 uint8_t default_awb_mode = mAwbModes[0];
803 if (std::count(mAwbModes.begin(), mAwbModes.end(),
804 ANDROID_CONTROL_AWB_MODE_AUTO)) {
805 default_awb_mode = ANDROID_CONTROL_AWB_MODE_AUTO;
806 } else if (std::count(mAwbModes.begin(), mAwbModes.end(),
807 ANDROID_CONTROL_AWB_MODE_OFF)) {
808 default_awb_mode = ANDROID_CONTROL_AWB_MODE_OFF;
809 }
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700810 res = base_metadata.update(ANDROID_CONTROL_AWB_MODE, &default_awb_mode, 1);
811 if (res != android::OK) {
812 return res;
813 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700814
815 // AWB regions not supported.
816
817 /* Other controls. */
818
819 uint8_t effect_mode = ANDROID_CONTROL_EFFECT_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700820 res = base_metadata.update(ANDROID_CONTROL_EFFECT_MODE, &effect_mode, 1);
821 if (res != android::OK) {
822 return res;
823 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700824
825 uint8_t control_mode = ANDROID_CONTROL_MODE_AUTO;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700826 res = base_metadata.update(ANDROID_CONTROL_MODE, &control_mode, 1);
827 if (res != android::OK) {
828 return res;
829 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700830
831 uint8_t scene_mode = ANDROID_CONTROL_SCENE_MODE_DISABLED;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700832 res = base_metadata.update(ANDROID_CONTROL_SCENE_MODE,
833 &scene_mode, 1);
834 if (res != android::OK) {
835 return res;
836 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700837
838 uint8_t video_stabilization = ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700839 res = base_metadata.update(ANDROID_CONTROL_VIDEO_STABILIZATION_MODE,
840 &video_stabilization, 1);
841 if (res != android::OK) {
842 return res;
843 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700844
845 // postRawSensitivityBoost: RAW not supported, leave null.
846
847 /* android.demosaic. */
848
849 // mode marked FUTURE.
850
851 /* android.edge. */
852
853 uint8_t edge_mode = ANDROID_EDGE_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700854 res = base_metadata.update(ANDROID_EDGE_MODE, &edge_mode, 1);
855 if (res != android::OK) {
856 return res;
857 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700858
859 // strength marked FUTURE.
860
861 /* android.flash. */
862
863 // firingPower, firingTime marked FUTURE.
864
865 uint8_t flash_mode = ANDROID_FLASH_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700866 res = base_metadata.update(ANDROID_FLASH_MODE, &flash_mode, 1);
867 if (res != android::OK) {
868 return res;
869 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700870
871 /* android.hotPixel. */
872
873 uint8_t hp_mode = ANDROID_HOT_PIXEL_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700874 res = base_metadata.update(ANDROID_HOT_PIXEL_MODE, &hp_mode, 1);
875 if (res != android::OK) {
876 return res;
877 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700878
879 /* android.jpeg. */
880
881 double gps_coords[] = {/*latitude*/0, /*longitude*/0, /*altitude*/0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700882 res = base_metadata.update(ANDROID_JPEG_GPS_COORDINATES, gps_coords, 3);
883 if (res != android::OK) {
884 return res;
885 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700886
887 uint8_t gps_processing_method[] = "none";
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700888 res = base_metadata.update(ANDROID_JPEG_GPS_PROCESSING_METHOD,
889 gps_processing_method,
890 ARRAY_SIZE(gps_processing_method));
891 if (res != android::OK) {
892 return res;
893 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700894
895 int64_t gps_timestamp = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700896 res = base_metadata.update(ANDROID_JPEG_GPS_TIMESTAMP, &gps_timestamp, 1);
897 if (res != android::OK) {
898 return res;
899 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700900
901 // JPEG orientation is relative to sensor orientation (mOrientation).
902 int32_t jpeg_orientation = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700903 res = base_metadata.update(ANDROID_JPEG_ORIENTATION, &jpeg_orientation, 1);
904 if (res != android::OK) {
905 return res;
906 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700907
908 // 1-100, larger is higher quality.
909 uint8_t jpeg_quality = 80;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700910 res = base_metadata.update(ANDROID_JPEG_QUALITY, &jpeg_quality, 1);
911 if (res != android::OK) {
912 return res;
913 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700914
915 // TODO(b/29580107): If thumbnail quality actually matters/can be adjusted,
916 // adjust this.
917 uint8_t thumbnail_quality = 80;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700918 res = base_metadata.update(ANDROID_JPEG_THUMBNAIL_QUALITY,
919 &thumbnail_quality, 1);
920 if (res != android::OK) {
921 return res;
922 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700923
924 // TODO(b/29580107): Choose a size matching the resolution.
925 int32_t thumbnail_size[] = {0, 0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700926 res = base_metadata.update(ANDROID_JPEG_THUMBNAIL_SIZE, thumbnail_size, 2);
927 if (res != android::OK) {
928 return res;
929 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700930
931 /* android.lens. */
932
933 // Fixed values.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700934 res = base_metadata.update(ANDROID_LENS_APERTURE, &mAperture, 1);
935 if (res != android::OK) {
936 return res;
937 }
938 res = base_metadata.update(ANDROID_LENS_FILTER_DENSITY, &mFilterDensity, 1);
939 if (res != android::OK) {
940 return res;
941 }
942 res = base_metadata.update(ANDROID_LENS_FOCAL_LENGTH, &mFocalLength, 1);
943 if (res != android::OK) {
944 return res;
945 }
946 res = base_metadata.update(ANDROID_LENS_FOCUS_DISTANCE, &mFocusDistance, 1);
947 if (res != android::OK) {
948 return res;
949 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700950
951 uint8_t optical_stabilization = ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700952 res = base_metadata.update(ANDROID_LENS_OPTICAL_STABILIZATION_MODE,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700953 &optical_stabilization, 1);
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700954 if (res != android::OK) {
955 return res;
956 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700957
958 /* android.noiseReduction. */
959
960 uint8_t noise_reduction_mode = ANDROID_NOISE_REDUCTION_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700961 res = base_metadata.update(ANDROID_NOISE_REDUCTION_MODE,
962 &noise_reduction_mode, 1);
963 if (res != android::OK) {
964 return res;
965 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700966
967 // strength marked FUTURE.
968
969 /* android.request. */
970
971 // Request Id unused by the HAL for now, and these are just
972 // templates, so just fill it in with a dummy.
973 int32_t id = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700974 res = base_metadata.update(ANDROID_REQUEST_ID, &id, 1);
975 if (res != android::OK) {
976 return res;
977 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700978
979 // metadataMode marked FUTURE.
980
981 /* android.scaler. */
982
983 // No cropping by default; use the full active array.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700984 res = base_metadata.update(ANDROID_SCALER_CROP_REGION, mPixelArraySize.data(),
985 mPixelArraySize.size());
986 if (res != android::OK) {
987 return res;
988 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700989
990 /* android.sensor. */
991
992 // exposureTime, sensitivity, testPattern[Data,Mode] not supported.
993
994 // Ignored when AE is OFF.
995 int64_t frame_duration = 33333333L; // 1/30 s.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700996 res = base_metadata.update(ANDROID_SENSOR_FRAME_DURATION, &frame_duration, 1);
997 if (res != android::OK) {
998 return res;
999 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001000
1001 /* android.shading. */
1002
1003 uint8_t shading_mode = ANDROID_SHADING_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001004 res = base_metadata.update(ANDROID_SHADING_MODE, &shading_mode, 1);
1005 if (res != android::OK) {
1006 return res;
1007 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001008
1009 /* android.statistics. */
1010
1011 uint8_t face_detect_mode = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001012 res = base_metadata.update(ANDROID_STATISTICS_FACE_DETECT_MODE,
1013 &face_detect_mode, 1);
1014 if (res != android::OK) {
1015 return res;
1016 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001017
1018 // histogramMode, sharpnessMapMode marked FUTURE.
1019
1020 uint8_t hp_map_mode = ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001021 res = base_metadata.update(ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE,
1022 &hp_map_mode, 1);
1023 if (res != android::OK) {
1024 return res;
1025 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001026
1027 uint8_t lens_shading_map_mode = ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001028 res = base_metadata.update(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE,
1029 &lens_shading_map_mode, 1);
1030 if (res != android::OK) {
1031 return res;
1032 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001033
1034 /* android.tonemap. */
1035
1036 // Tonemap only required for MANUAL_POST_PROCESSING capability.
1037
1038 /* android.led. */
1039
1040 uint8_t transmit = ANDROID_LED_TRANSMIT_ON;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001041 res = base_metadata.update(ANDROID_LED_TRANSMIT, &transmit, 1);
1042 if (res != android::OK) {
1043 return res;
1044 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001045
1046 /* android.reprocess */
1047
1048 // Only needed for REPROCESS capability.
1049
1050
1051 /* Template variable values. */
1052
1053 // Find the FPS ranges "closest" to a desired range
1054 // (minimum abs distance from min to min and max to max).
1055 // Find both a fixed rate and a variable rate, for different purposes.
1056 std::array<int32_t, 2> desired_flat_fps_range = {{30, 30}};
1057 std::array<int32_t, 2> desired_variable_fps_range = {{5, 30}};
1058 std::array<int32_t, 2> flat_fps_range;
1059 std::array<int32_t, 2> variable_fps_range;
1060 int32_t best_flat_distance = std::numeric_limits<int32_t>::max();
1061 int32_t best_variable_distance = std::numeric_limits<int32_t>::max();
1062 size_t num_fps_ranges = mFpsRanges.num_arrays();
1063 for (size_t i = 0; i < num_fps_ranges; ++i) {
1064 const int32_t* range = mFpsRanges[i];
1065 // Variable fps.
1066 int32_t distance = std::abs(range[0] - desired_variable_fps_range[0]) +
1067 std::abs(range[1] - desired_variable_fps_range[1]);
1068 if (distance < best_variable_distance) {
1069 variable_fps_range[0] = range[0];
1070 variable_fps_range[1] = range[1];
1071 best_variable_distance = distance;
1072 }
1073 // Flat fps. Only do if range is actually flat.
1074 // Note at least one flat range is required,
1075 // so something will always be filled in.
1076 if (range[0] == range[1]) {
1077 distance = std::abs(range[0] - desired_flat_fps_range[0]) +
1078 std::abs(range[1] - desired_flat_fps_range[1]);
1079 if (distance < best_flat_distance) {
1080 flat_fps_range[0] = range[0];
1081 flat_fps_range[1] = range[1];
1082 best_flat_distance = distance;
1083 }
1084 }
1085 }
1086
1087 // Priority: continuous > auto > off > whatever is available.
1088 bool continuous_still_avail = std::count(
1089 mAfModes.begin(), mAfModes.end(),
1090 ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE);
1091 bool continuous_video_avail = std::count(
1092 mAfModes.begin(), mAfModes.end(),
1093 ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO);
1094 uint8_t non_continuous_af_mode = mAfModes[0];
1095 if (std::count(mAfModes.begin(), mAfModes.end(),
1096 ANDROID_CONTROL_AF_MODE_AUTO)) {
1097 non_continuous_af_mode = ANDROID_CONTROL_AF_MODE_AUTO;
1098 } else if (std::count(mAfModes.begin(), mAfModes.end(),
1099 ANDROID_CONTROL_AF_MODE_OFF)) {
1100 non_continuous_af_mode = ANDROID_CONTROL_AF_MODE_OFF;
1101 }
1102 uint8_t still_af_mode = continuous_still_avail ?
1103 ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE : non_continuous_af_mode;
1104 uint8_t video_af_mode = continuous_video_avail ?
1105 ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO : non_continuous_af_mode;
1106
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001107 for (uint8_t template_id = 1; template_id < CAMERA3_TEMPLATE_COUNT;
1108 ++template_id) {
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001109 // General differences/support.
1110 uint8_t intent;
1111 uint8_t af_mode;
1112 std::array<int32_t, 2> fps_range;
1113 switch(template_id) {
1114 case CAMERA3_TEMPLATE_PREVIEW:
1115 intent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW;
1116 af_mode = still_af_mode;
1117 fps_range = flat_fps_range;
1118 break;
1119 case CAMERA3_TEMPLATE_STILL_CAPTURE:
1120 intent = ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE;
1121 af_mode = still_af_mode;
1122 fps_range = variable_fps_range;
1123 break;
1124 case CAMERA3_TEMPLATE_VIDEO_RECORD:
1125 intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD;
1126 af_mode = video_af_mode;
1127 fps_range = flat_fps_range;
1128 break;
1129 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
1130 intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT;
1131 af_mode = video_af_mode;
1132 fps_range = flat_fps_range;
1133 break;
1134 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG: // Fall through.
1135 case CAMERA3_TEMPLATE_MANUAL: // Fall though.
1136 default:
1137 // Unsupported/unrecognized. Don't add this template; skip it.
1138 continue;
1139 }
1140
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001141 // Copy our base metadata and add the new items.
1142 android::CameraMetadata template_metadata(base_metadata);
1143 res = template_metadata.update(ANDROID_CONTROL_CAPTURE_INTENT, &intent, 1);
1144 if (res != android::OK) {
1145 return res;
1146 }
1147 res = template_metadata.update(ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
1148 fps_range.data(), fps_range.size());
1149 if (res != android::OK) {
1150 return res;
1151 }
1152 res = template_metadata.update(ANDROID_CONTROL_AF_MODE, &af_mode, 1);
1153 if (res != android::OK) {
1154 return res;
1155 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001156
1157 const camera_metadata_t* template_raw_metadata =
1158 template_metadata.getAndLock();
1159 res = setTemplate(template_id, template_raw_metadata);
1160 if (res != android::OK) {
1161 return res;
1162 }
1163 res = template_metadata.unlock(template_raw_metadata);
1164 if (res != android::OK) {
1165 return res;
1166 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001167 }
1168
1169 mTemplatesInitialized = true;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001170 return 0;
1171}
1172
1173bool V4L2Camera::isValidCaptureSettings(const camera_metadata_t* settings) {
1174 HAL_LOG_ENTER();
1175
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001176 // TODO(b/29335262): reject capture settings this camera isn't capable of.
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001177 return true;
1178}
1179
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001180int V4L2Camera::initCharacteristics() {
1181 HAL_LOG_ENTER();
1182
1183 /* Physical characteristics. */
1184 // No way to get these in V4L2, so faked.
1185 // Note: While many of these are primarily informative for post-processing
1186 // calculations by the app and will potentially cause bad results there,
1187 // focal length and physical size are actually used in framework
1188 // calculations (field of view, pixel pitch, etc), so faking them may
1189 // have unexpected results.
1190 mAperture = 2.0; // RPi camera v2 is f/2.0.
1191 mFilterDensity = 0.0;
1192 mFocalLength = 3.04; // RPi camera v2 is 3.04mm.
1193 mOrientation = 0;
1194 mPhysicalSize = {{3.674, 2.760}}; // RPi camera v2 is 3.674 x 2.760 mm.
1195
1196 /* Fixed features. */
1197
1198 // TODO(b/29394024): query VIDIOC_CROPCAP to get pixel rectangle.
1199 // Spoofing as 640 x 480 for now.
1200 mPixelArraySize = {{/*xmin*/0, /*ymin*/0, /*width*/640, /*height*/480}};
1201
1202 // V4L2 VIDIOC_CROPCAP doesn't give a way to query this;
1203 // it's driver dependent. For now, assume freeform, and
1204 // some cameras may just behave badly.
1205 // TODO(b/29579652): Figure out a way to determine this.
1206 mCropType = ANDROID_SCALER_CROPPING_TYPE_FREEFORM;
1207
1208 // TODO(b/29394024): query VIDIOC_CROPCAP to get cropping ranges,
1209 // and VIDIOC_G_CROP to determine if cropping is supported.
1210 // If the ioctl isn't available (or cropping has non-square pixelaspect),
1211 // assume no cropping/scaling.
1212 // May need to try setting some crops to determine what the driver actually
1213 // supports (including testing center vs freeform).
1214 mMaxZoom = 1;
1215
1216 // TODO(b/29394024): query V4L2_CID_EXPOSURE_BIAS.
1217 mAeCompensationRange = {{0, 0}};
1218 mAeCompensationStep = {1, 1};
1219
1220 // TODO(b/29394024): query V4L2_CID_3A_LOCK.
1221 mAeLockAvailable = ANDROID_CONTROL_AE_LOCK_AVAILABLE_FALSE;
1222 mAwbLockAvailable = ANDROID_CONTROL_AWB_LOCK_AVAILABLE_FALSE;
1223
1224 // TODO(b/29394024): query V4L2_CID_FLASH_LED_MODE.
1225 mFlashAvailable = 0;
1226
1227 // TODO(b/29394024): query V4L2_CID_FOCUS_ABSOLUTE for focus range.
1228 mFocusDistance = 0; // Fixed focus.
1229
1230 /* Features with (potentially) multiple options. */
1231
1232 // TODO(b/29394024): query V4L2_CID_EXPOSURE_AUTO for ae modes.
1233 mAeModes.push_back(ANDROID_CONTROL_AE_MODE_ON);
1234
1235 // TODO(b/29394024): query V4L2_CID_POWER_LINE_FREQUENCY.
1236 // Auto as the default, since it could mean anything, while OFF would
1237 // require guaranteeing no antibanding happens.
1238 mAeAntibandingModes.push_back(ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO);
1239
1240 // TODO(b/29394024): query V4L2_CID_FOCUS_AUTO for
1241 // CONTINUOUS_VIDEO/CONTINUOUS_PICTURE. V4L2_CID_AUTO_FOCUS_START
1242 // supports what Android thinks of as auto focus (single auto focus).
1243 // V4L2_CID_AUTO_FOCUS_RANGE allows MACRO.
1244 mAfModes.push_back(ANDROID_CONTROL_AF_MODE_OFF);
1245
1246 // TODO(b/29394024): query V4L2_CID_AUTO_WHITE_BALANCE, or
1247 // V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE if available.
1248 mAwbModes.push_back(ANDROID_CONTROL_AWB_MODE_AUTO);
1249
1250 // TODO(b/29394024): query V4L2_CID_SCENE_MODE.
1251 mSceneModes.push_back(ANDROID_CONTROL_SCENE_MODE_DISABLED);
1252
1253 mControlModes.push_back(ANDROID_CONTROL_MODE_AUTO);
1254 if (mSceneModes.size() > 1) {
1255 // We have some mode other than just DISABLED available.
1256 mControlModes.push_back(ANDROID_CONTROL_MODE_USE_SCENE_MODE);
1257 }
1258
1259 // TODO(b/29394024): query V4L2_CID_COLORFX.
1260 mEffects.push_back(ANDROID_CONTROL_EFFECT_MODE_OFF);
1261
1262 // TODO(b/29394024): query V4L2_CID_FLASH_INDICATOR_INTENSITY.
1263 // For now, no indicator LED available; nothing to push back.
1264 // When there is, push back ANDROID_LED_AVAILABLE_LEDS_TRANSMIT.
1265
1266 // TODO(b/29394024): query V4L2_CID_IMAGE_STABILIZATION.
1267 mOpticalStabilizationModes.push_back(
1268 ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF);
1269 mVideoStabilizationModes.push_back(
1270 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF);
1271
1272 // Need to support YUV_420_888 and JPEG.
1273 // TODO(b/29394024): query VIDIOC_ENUM_FMT.
1274 std::vector<uint32_t> formats = {{V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420}};
1275 int32_t hal_format;
1276 // We want to find the smallest maximum frame duration amongst formats.
1277 mMaxFrameDuration = std::numeric_limits<int64_t>::max();
1278 int64_t min_yuv_frame_duration = std::numeric_limits<int64_t>::max();
1279 for (auto format : formats) {
1280 // Translate V4L2 format to HAL format.
1281 switch (format) {
1282 case V4L2_PIX_FMT_JPEG:
1283 hal_format = HAL_PIXEL_FORMAT_BLOB;
1284 break;
1285 case V4L2_PIX_FMT_YUV420:
1286 hal_format = HAL_PIXEL_FORMAT_YCbCr_420_888;
1287 default:
1288 // Unrecognized/unused format. Skip it.
1289 continue;
1290 }
1291
1292 // TODO(b/29394024): query VIDIOC_ENUM_FRAMESIZES.
1293 // For now, just 640x480.
1294 ArrayVector<int32_t, 2> frame_sizes;
1295 frame_sizes.push_back({{640, 480}});
1296 size_t num_frame_sizes = frame_sizes.num_arrays();
1297 for (size_t i = 0; i < num_frame_sizes; ++i) {
1298 const int32_t* frame_size = frame_sizes[i];
1299 mStreamConfigs.push_back({{hal_format, frame_size[0], frame_size[1],
1300 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT}});
1301
1302 // TODO(b/29394024): query VIDIOC_ENUM_FRAMEINTERVALS.
1303 // For now, using the emulator max value of .3 sec.
1304 int64_t max_frame_duration = 300000000;
1305 // For now, min value of 30 fps = 1/30 spf ~= 33333333 ns.
1306 // For whatever reason the goldfish/qcom cameras report this as
1307 // 33331760, so copying that.
1308 int64_t min_frame_duration = 33331760;
1309
1310 mMinFrameDurations.push_back(
1311 {{hal_format, frame_size[0], frame_size[1], min_frame_duration}});
1312
1313 // In theory max frame duration (min frame rate) should be consistent
1314 // between all formats, but we check and only advertise the smallest
1315 // available max duration just in case.
1316 if (max_frame_duration < mMaxFrameDuration) {
1317 mMaxFrameDuration = max_frame_duration;
1318 }
1319
1320 // We only care about min frame duration (max frame rate) for YUV.
1321 if (hal_format == HAL_PIXEL_FORMAT_YCbCr_420_888 &&
1322 min_frame_duration < min_yuv_frame_duration) {
1323 min_yuv_frame_duration = min_frame_duration;
1324 }
1325
1326 // Usually 0 for non-jpeg, non-zero for JPEG.
1327 // Randomly choosing absurd 1 sec for JPEG. Unsure what this breaks.
1328 int64_t stall_duration = 0;
1329 if (hal_format == HAL_PIXEL_FORMAT_BLOB) {
1330 stall_duration = 1000000000;
1331 }
1332 mStallDurations.push_back(
1333 {{hal_format, frame_size[0], frame_size[1], stall_duration}});
1334 }
1335 }
1336
1337 // This should be at minimum {mi, ma}, {ma, ma} where mi and ma
1338 // are min and max frame rates for YUV_420_888. Min should be at most 15.
1339 // Convert from frame durations measured in ns.
1340 int32_t min_yuv_fps = 1000000000 / mMaxFrameDuration;
1341 if (min_yuv_fps > 15) {
1342 return -ENODEV;
1343 }
1344 int32_t max_yuv_fps = 1000000000 / min_yuv_frame_duration;
1345 mFpsRanges.push_back({{min_yuv_fps, max_yuv_fps}});
1346 mFpsRanges.push_back({{max_yuv_fps, max_yuv_fps}});
1347 // Always advertise {30, 30} if max is even higher,
1348 // since this is what the default video requests use.
1349 if (max_yuv_fps > 30) {
1350 mFpsRanges.push_back({{30, 30}});
1351 }
1352
1353 mCharacteristicsInitialized = true;
1354 return 0;
1355}
1356
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001357} // namespace v4l2_camera_hal