blob: f8c1c610a2beaddc05e49847cfd6e0ee76208550 [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
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070017#include "v4l2_camera.h"
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070018
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
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070030#include "common.h"
31#include "stream_format.h"
32#include "v4l2_gralloc.h"
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070033
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070034#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
35
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070036namespace v4l2_camera_hal {
37
Ari Hausman-Cohendde80172016-07-01 16:20:36 -070038// Helper function for managing metadata.
39static std::vector<int32_t> getMetadataKeys(
40 const camera_metadata_t* metadata) {
41 std::vector<int32_t> keys;
42 size_t num_entries = get_camera_metadata_entry_count(metadata);
43 for (size_t i = 0; i < num_entries; ++i) {
44 camera_metadata_ro_entry_t entry;
45 get_camera_metadata_ro_entry(metadata, i, &entry);
46 keys.push_back(entry.tag);
47 }
48 return keys;
49}
50
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070051V4L2Camera* V4L2Camera::NewV4L2Camera(int id, const std::string path) {
52 HAL_LOG_ENTER();
53
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -070054 std::unique_ptr<V4L2Wrapper> v4l2_wrapper(V4L2Wrapper::NewV4L2Wrapper(path));
55 if (!v4l2_wrapper) {
56 HAL_LOGE("Failed to initialize V4L2 wrapper.");
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070057 return nullptr;
58 }
59
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -070060 return new V4L2Camera(id, std::move(v4l2_wrapper));
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070061}
62
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -070063V4L2Camera::V4L2Camera(int id, std::unique_ptr<V4L2Wrapper> v4l2_wrapper)
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070064 : default_camera_hal::Camera(id),
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -070065 mV4L2Device(std::move(v4l2_wrapper)),
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070066 mTemplatesInitialized(false),
67 mCharacteristicsInitialized(false) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070068 HAL_LOG_ENTER();
69}
70
71V4L2Camera::~V4L2Camera() {
72 HAL_LOG_ENTER();
73}
74
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070075int V4L2Camera::connect() {
76 HAL_LOG_ENTER();
77
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -070078 int res = mV4L2Device->Connect();
79 if (res) {
80 HAL_LOGE("Failed to connect to device.");
81 return res;
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070082 }
83
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070084 // TODO(b/29185945): confirm this is a supported device.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070085 // This is checked by the HAL, but the device at mDevicePath may
86 // not be the same one that was there when the HAL was loaded.
87 // (Alternatively, better hotplugging support may make this unecessary
88 // by disabling cameras that get disconnected and checking newly connected
89 // cameras, so connect() is never called on an unsupported camera)
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070090
91 // TODO(b/29158098): Inform service of any flashes that are no longer available
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070092 // because this camera is in use.
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070093 return 0;
94}
95
96void V4L2Camera::disconnect() {
97 HAL_LOG_ENTER();
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -070098
99 mV4L2Device->Disconnect();
100
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700101 // TODO(b/29158098): Inform service of any flashes that are available again
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700102 // because this camera is no longer in use.
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -0700103}
104
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700105int V4L2Camera::initStaticInfo(camera_metadata_t** out) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700106 HAL_LOG_ENTER();
107
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700108 android::status_t res;
109 // Device characteristics need to be queried prior
110 // to static info setup.
111 if (!mCharacteristicsInitialized) {
112 res = initCharacteristics();
113 if (res) {
114 return res;
115 }
116 }
117
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700118 android::CameraMetadata info;
Ari Hausman-Cohen63f69822016-06-10 11:40:35 -0700119
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700120 // Static metadata characteristics from /system/media/camera/docs/docs.html.
121
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700122 /* android.colorCorrection. */
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700123
124 // No easy way to turn chromatic aberration correction OFF in v4l2,
125 // though this may be supportable via a collection of other user controls.
126 uint8_t avail_aberration_modes[] = {
127 ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST,
128 ANDROID_COLOR_CORRECTION_ABERRATION_MODE_HIGH_QUALITY};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700129 res = info.update(ANDROID_COLOR_CORRECTION_AVAILABLE_ABERRATION_MODES,
130 avail_aberration_modes, ARRAY_SIZE(avail_aberration_modes));
131 if (res != android::OK) {
132 return res;
133 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700134
135 /* android.control. */
136
137 /* 3As */
138
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700139 res = info.update(ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES,
140 mAeAntibandingModes.data(), mAeAntibandingModes.size());
141 if (res != android::OK) {
142 return res;
143 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700144
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700145 res = info.update(ANDROID_CONTROL_AE_AVAILABLE_MODES,
146 mAeModes.data(), mAeModes.size());
147 if (res != android::OK) {
148 return res;
149 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700150
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700151 // Flatten mFpsRanges.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700152 res = info.update(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES,
153 mFpsRanges.data(), mFpsRanges.total_num_elements());
154 if (res != android::OK) {
155 return res;
156 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700157
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700158 res = info.update(ANDROID_CONTROL_AE_COMPENSATION_RANGE,
159 mAeCompensationRange.data(), mAeCompensationRange.size());
160 if (res != android::OK) {
161 return res;
162 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700163
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700164 res = info.update(ANDROID_CONTROL_AE_COMPENSATION_STEP,
165 &mAeCompensationStep, 1);
166 if (res != android::OK) {
167 return res;
168 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700169
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700170 res = info.update(ANDROID_CONTROL_AF_AVAILABLE_MODES,
171 mAfModes.data(), mAfModes.size());
172 if (res != android::OK) {
173 return res;
174 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700175
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700176 res = info.update(ANDROID_CONTROL_AWB_AVAILABLE_MODES,
177 mAwbModes.data(), mAwbModes.size());
178 if (res != android::OK) {
179 return res;
180 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700181
182 // Couldn't find any V4L2 support for regions, though maybe it's out there.
183 int32_t max_regions[] = {/*AE*/ 0,/*AWB*/ 0,/*AF*/ 0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700184 res = info.update(ANDROID_CONTROL_MAX_REGIONS,
185 max_regions, ARRAY_SIZE(max_regions));
186 if (res != android::OK) {
187 return res;
188 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700189
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700190 res = info.update(ANDROID_CONTROL_AE_LOCK_AVAILABLE,
191 &mAeLockAvailable, 1);
192 if (res != android::OK) {
193 return res;
194 }
195 res = info.update(ANDROID_CONTROL_AWB_LOCK_AVAILABLE,
196 &mAwbLockAvailable, 1);
197 if (res != android::OK) {
198 return res;
199 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700200
201 /* Scene modes. */
202
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700203 res = info.update(ANDROID_CONTROL_AVAILABLE_SCENE_MODES,
204 mSceneModes.data(), mSceneModes.size());
205 if (res != android::OK) {
206 return res;
207 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700208
209 // A 3-tuple of AE, AWB, AF overrides for each scene mode.
210 // Ignored for DISABLED, FACE_PRIORITY and FACE_PRIORITY_LOW_LIGHT.
211 uint8_t scene_mode_overrides[] = {
212 /*SCENE_MODE_DISABLED*/ /*AE*/0, /*AW*/0, /*AF*/0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700213 res = info.update(ANDROID_CONTROL_SCENE_MODE_OVERRIDES,
214 scene_mode_overrides, ARRAY_SIZE(scene_mode_overrides));
215 if (res != android::OK) {
216 return res;
217 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700218
219 /* Top level 3A/Scenes switch. */
220
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700221 res = info.update(ANDROID_CONTROL_AVAILABLE_MODES,
222 mControlModes.data(), mControlModes.size());
223 if (res != android::OK) {
224 return res;
225 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700226
227 /* Other android.control configuration. */
228
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700229 res = info.update(ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES,
230 mVideoStabilizationModes.data(),
231 mVideoStabilizationModes.size());
232 if (res != android::OK) {
233 return res;
234 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700235
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700236 res = info.update(ANDROID_CONTROL_AVAILABLE_EFFECTS,
237 mEffects.data(), mEffects.size());
238 if (res != android::OK) {
239 return res;
240 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700241
242 // AVAILABLE_HIGH_SPEED_VIDEO_CONFIGURATIONS only necessary
243 // for devices supporting CONSTRAINED_HIGH_SPEED_VIDEO,
244 // which this HAL doesn't support.
245
246 // POST_RAW_SENSITIVITY_BOOST_RANGE only necessary
247 // for devices supporting RAW format outputs.
248
249 /* android.edge. */
250
251 // Not sure if V4L2 does or doesn't do this, but HAL documentation says
252 // all devices must support FAST, and FAST can be equivalent to OFF, so
253 // either way it's fine to list.
254 uint8_t avail_edge_modes[] = {
255 ANDROID_EDGE_MODE_FAST};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700256 res = info.update(ANDROID_EDGE_AVAILABLE_EDGE_MODES,
257 avail_edge_modes, ARRAY_SIZE(avail_edge_modes));
258 if (res != android::OK) {
259 return res;
260 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700261
262 /* android.flash. */
263
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700264 res = info.update(ANDROID_FLASH_INFO_AVAILABLE,
265 &mFlashAvailable, 1);
266 if (res != android::OK) {
267 return res;
268 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700269
270 // info.chargeDuration, color.Temperature, maxEnergy marked FUTURE.
271
272 /* android.hotPixel. */
273
274 // No known V4L2 hot pixel correction. But it might be happening,
275 // so we report FAST/HIGH_QUALITY.
276 uint8_t avail_hot_pixel_modes[] = {
277 ANDROID_HOT_PIXEL_MODE_FAST,
278 ANDROID_HOT_PIXEL_MODE_HIGH_QUALITY};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700279 res = info.update(ANDROID_HOT_PIXEL_AVAILABLE_HOT_PIXEL_MODES,
280 avail_hot_pixel_modes, ARRAY_SIZE(avail_hot_pixel_modes));
281 if (res != android::OK) {
282 return res;
283 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700284
285 /* android.jpeg. */
286
287 // For now, no thumbnails available (only [0,0], the "no thumbnail" size).
288 // TODO(b/29580107): Could end up with a mismatch between request & result,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700289 // since V4L2 doesn't actually allow for thumbnail size control.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700290 int32_t thumbnail_sizes[] = {
291 0, 0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700292 res = info.update(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES,
293 thumbnail_sizes, ARRAY_SIZE(thumbnail_sizes));
294 if (res != android::OK) {
295 return res;
296 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700297
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700298 // V4L2 can query this with VIDIOC_TRY_FMT (or VIDIOC_S_FMT if TRY
299 // isn't supported), reading the fmt.pix.sizeimage for the largest
300 // jpeg size. For now use a constant (defined in V4L2Gralloc.h).
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700301 int32_t max_jpeg_size = V4L2_MAX_JPEG_SIZE;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700302 res = info.update(ANDROID_JPEG_MAX_SIZE,
303 &max_jpeg_size, 1);
304 if (res != android::OK) {
305 return res;
306 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700307
308 /* android.lens. */
309
310 /* Misc. lens control. */
311
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700312 res = info.update(ANDROID_LENS_INFO_AVAILABLE_APERTURES,
313 &mAperture, 1);
314 if (res != android::OK) {
315 return res;
316 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700317
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700318 res = info.update(ANDROID_LENS_INFO_AVAILABLE_FILTER_DENSITIES,
319 &mFilterDensity, 1);
320 if (res != android::OK) {
321 return res;
322 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700323
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700324 res = info.update(ANDROID_LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION,
325 mOpticalStabilizationModes.data(),
326 mOpticalStabilizationModes.size());
327 if (res != android::OK) {
328 return res;
329 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700330
Ari Hausman-Cohenab3a0a42016-08-02 11:10:56 -0700331 // lens.info.shadingMapSize not required for non-full devices.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700332
333 // All V4L2 devices are considered to be external facing.
334 uint8_t facing = ANDROID_LENS_FACING_EXTERNAL;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700335 res = info.update(ANDROID_LENS_FACING, &facing, 1);
336 if (res != android::OK) {
337 return res;
338 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700339
340 /* Zoom/Focus. */
341
342 // No way to actually get the focal length in V4L2, but it's a required key,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700343 // so we just fake it.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700344 res = info.update(ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS,
345 &mFocalLength, 1);
346 if (res != android::OK) {
347 return res;
348 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700349
350 // V4L2 focal units do not correspond to a particular physical unit.
351 uint8_t focus_calibration =
352 ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION_UNCALIBRATED;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700353 res = info.update(ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION,
354 &focus_calibration, 1);
355 if (res != android::OK) {
356 return res;
357 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700358
359 // info.hyperfocalDistance not required for UNCALIBRATED.
360
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700361 res = info.update(ANDROID_LENS_INFO_MINIMUM_FOCUS_DISTANCE,
362 &mFocusDistance, 1);
363 if (res != android::OK) {
364 return res;
365 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700366
367 /* Depth. */
368
369 // DEPTH capability not supported by this HAL. Not implemented:
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700370 // poseRotation
371 // poseTranslation
372 // intrinsicCalibration
373 // radialDistortion
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700374
375 /* anroid.noise. */
376
377 // Unable to control noise reduction in V4L2 devices,
378 // but FAST is allowed to be the same as OFF.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700379 uint8_t avail_noise_reduction_modes[] = {ANDROID_NOISE_REDUCTION_MODE_FAST};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700380 res = info.update(ANDROID_NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES,
381 avail_noise_reduction_modes,
382 ARRAY_SIZE(avail_noise_reduction_modes));
383 if (res != android::OK) {
384 return res;
385 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700386
387 /* android.request. */
388
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700389 int32_t max_num_output_streams[] = {mMaxRawOutputStreams,
390 mMaxNonStallingOutputStreams,
391 mMaxStallingOutputStreams};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700392 res = info.update(ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS,
393 max_num_output_streams, ARRAY_SIZE(max_num_output_streams));
394 if (res != android::OK) {
395 return res;
396 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700397
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700398 res = info.update(ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS,
399 &mMaxInputStreams, 1);
400 if (res != android::OK) {
401 return res;
402 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700403
404 // No way to know for V4L2, so fake with max allowable latency.
405 // Doesn't mean much without per-frame controls.
406 uint8_t pipeline_max_depth = 4;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700407 res = info.update(ANDROID_REQUEST_PIPELINE_MAX_DEPTH,
408 &pipeline_max_depth, 1);
409 if (res != android::OK) {
410 return res;
411 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700412
413 // Partial results not supported; partialResultCount defaults to 1.
414
415 // Available capabilities & keys queried at very end of this method.
416
417 /* android.scaler. */
418
419 /* Cropping. */
420
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700421 res = info.update(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
422 &mMaxZoom, 1);
423 if (res != android::OK) {
424 return res;
425 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700426
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700427 res = info.update(ANDROID_SCALER_CROPPING_TYPE, &mCropType, 1);
428 if (res != android::OK) {
429 return res;
430 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700431
432 /* Streams. */
433
434 // availableInputOutputFormatsMap only required for reprocessing capability.
435
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700436 // Flatten mStreamConfigs.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700437 res = info.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
438 mStreamConfigs.data(),
439 mStreamConfigs.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 mMinFrameDurations.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700445 res = info.update(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS,
446 mMinFrameDurations.data(),
447 mMinFrameDurations.total_num_elements());
448 if (res != android::OK) {
449 return res;
450 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700451
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700452 // Flatten mStallDurations.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700453 res = info.update(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS,
454 mStallDurations.data(),
455 mStallDurations.total_num_elements());
456 if (res != android::OK) {
457 return res;
458 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700459
460 /* android.sensor. */
461
462 /* Sizes. */
463
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700464 res = info.update(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE,
465 mPixelArraySize.data(), mPixelArraySize.size());
466 if (res != android::OK) {
467 return res;
468 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700469 // No V4L2 way to differentiate active vs. inactive parts of the rectangle.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700470 res = info.update(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE,
471 mPixelArraySize.data(), mPixelArraySize.size());
472 if (res != android::OK) {
473 return res;
474 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700475
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700476 res = info.update(ANDROID_SENSOR_INFO_PHYSICAL_SIZE,
477 mPhysicalSize.data(), mPhysicalSize.size());
478 if (res != android::OK) {
479 return res;
480 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700481
482 /* Misc sensor information. */
483
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700484 res = info.update(ANDROID_SENSOR_INFO_MAX_FRAME_DURATION,
485 &mMaxFrameDuration, 1);
486 if (res != android::OK) {
487 return res;
488 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700489
490 // HAL uses BOOTTIME timestamps.
491 // TODO(b/29457051): make sure timestamps are consistent throughout the HAL.
492 uint8_t timestamp_source = ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700493 res = info.update(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE,
494 &timestamp_source, 1);
495 if (res != android::OK) {
496 return res;
497 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700498
499 // As in initDeviceInfo, no way to actually get orientation.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700500 res = info.update(ANDROID_SENSOR_ORIENTATION, &mOrientation, 1);
501 if (res != android::OK) {
502 return res;
503 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700504
505 // availableTestPatternModes just defaults to OFF, which is fine.
506
507 // info.exposureTimeRange, info.sensitivityRange:
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700508 // exposure/sensitivity manual control not supported.
509 // Could query V4L2_CID_ISO_SENSITIVITY to support sensitivity if desired.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700510
511 // info.whiteLevel, info.lensShadingApplied,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700512 // info.preCorrectionPixelArraySize, referenceIlluminant1/2,
513 // calibrationTransform1/2, colorTransform1/2, forwardMatrix1/2,
514 // blackLevelPattern, profileHueSatMapDimensions
515 // all only necessary for RAW.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700516
517 // baseGainFactor marked FUTURE.
518
519 // maxAnalogSensitivity optional for LIMITED device.
520
521 // opticalBlackRegions: No known way to get in V4L2, but not necessary.
522
523 // opaqueRawSize not necessary since RAW_OPAQUE format not supported.
524
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700525 /* android.shading */
526
527 // No known V4L2 lens shading. But it might be happening,
528 // so we report FAST/HIGH_QUALITY.
529 uint8_t avail_shading_modes[] = {
530 ANDROID_SHADING_MODE_FAST,
531 ANDROID_SHADING_MODE_HIGH_QUALITY};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700532 res = info.update(ANDROID_SHADING_AVAILABLE_MODES,
533 avail_shading_modes, ARRAY_SIZE(avail_shading_modes));
534 if (res != android::OK) {
535 return res;
536 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700537
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700538 /* android.statistics */
539
540 // Face detection not supported.
541 uint8_t avail_face_detect_modes[] = {
542 ANDROID_STATISTICS_FACE_DETECT_MODE_OFF};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700543 res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES,
544 avail_face_detect_modes,
545 ARRAY_SIZE(avail_face_detect_modes));
546 if (res != android::OK) {
547 return res;
548 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700549
550 int32_t max_face_count = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700551 res = info.update(ANDROID_STATISTICS_INFO_MAX_FACE_COUNT,
552 &max_face_count, 1);
553 if (res != android::OK) {
554 return res;
555 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700556
557 // info.histogramBucketCount, info.maxHistogramCount,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700558 // info.maxSharpnessMapValue, info.sharpnessMapSizemarked FUTURE.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700559
560 // ON only needs to be supported for RAW capable devices.
561 uint8_t avail_hot_pixel_map_modes[] = {
562 ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700563 res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_HOT_PIXEL_MAP_MODES,
564 avail_hot_pixel_map_modes,
565 ARRAY_SIZE(avail_hot_pixel_map_modes));
566 if (res != android::OK) {
567 return res;
568 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700569
570 // ON only needs to be supported for RAW capable devices.
571 uint8_t avail_lens_shading_map_modes[] = {
572 ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700573 res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES,
574 avail_lens_shading_map_modes,
575 ARRAY_SIZE(avail_lens_shading_map_modes));
576 if (res != android::OK) {
577 return res;
578 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700579
580 /* android.tonemap. */
581
582 // tonemapping only required for MANUAL_POST_PROCESSING capability.
583
584 /* android.led. */
585
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700586 // May or may not have LEDs available.
587 if (!mLeds.empty()) {
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700588 res = info.update(ANDROID_LED_AVAILABLE_LEDS, mLeds.data(), mLeds.size());
589 if (res != android::OK) {
590 return res;
591 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700592 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700593
594 /* android.sync. */
595
596 // "LIMITED devices are strongly encouraged to use a non-negative value.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700597 // If UNKNOWN is used here then app developers do not have a way to know
598 // when sensor settings have been applied." - Unfortunately, V4L2 doesn't
599 // really help here either. Could even be that adjusting settings mid-stream
600 // blocks in V4L2, and should be avoided.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700601 int32_t max_latency = ANDROID_SYNC_MAX_LATENCY_UNKNOWN;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700602 res = info.update(ANDROID_SYNC_MAX_LATENCY,
603 &max_latency, 1);
604 if (res != android::OK) {
605 return res;
606 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700607
608 /* android.reprocess. */
609
610 // REPROCESSING not supported by this HAL.
611
612 /* android.depth. */
613
614 // DEPTH not supported by this HAL.
615
616 /* Capabilities and android.info. */
617
618 uint8_t hw_level = ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700619 res = info.update(ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL,
620 &hw_level, 1);
621 if (res != android::OK) {
622 return res;
623 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700624
625 uint8_t capabilities[] = {
626 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700627 res = info.update(ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
628 capabilities, ARRAY_SIZE(capabilities));
629 if (res != android::OK) {
630 return res;
631 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700632
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700633 // Scan a default request template for included request keys.
634 if (!mTemplatesInitialized) {
635 res = initTemplates();
636 if (res) {
637 return res;
638 }
639 }
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700640 const camera_metadata_t* preview_request = nullptr;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700641 // Search templates from the beginning for a supported one.
642 for (uint8_t template_id = 1; template_id < CAMERA3_TEMPLATE_COUNT;
643 ++template_id) {
644 preview_request = constructDefaultRequestSettings(template_id);
645 if (preview_request != nullptr) {
646 break;
647 }
648 }
649 if (preview_request == nullptr) {
650 HAL_LOGE("No valid templates, can't get request keys.");
651 return -ENODEV;
652 }
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700653 std::vector<int32_t> avail_request_keys = getMetadataKeys(preview_request);
654 res = info.update(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS,
655 avail_request_keys.data(), avail_request_keys.size());
656 if (res != android::OK) {
657 return res;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700658 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700659
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700660 // Result keys will be duplicated from the request, plus a few extras.
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700661 // TODO(b/30035628): additonal available result keys.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700662 std::vector<int32_t> avail_result_keys(avail_request_keys);
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700663 res = info.update(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS,
664 avail_result_keys.data(), avail_result_keys.size());
665 if (res != android::OK) {
666 return res;
667 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700668
669 // Last thing, once all the available characteristics have been added.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700670 const camera_metadata_t* static_characteristics = info.getAndLock();
671 std::vector<int32_t> avail_characteristics_keys =
672 getMetadataKeys(static_characteristics);
673 res = info.unlock(static_characteristics);
674 if (res != android::OK) {
675 return res;
676 }
677 avail_characteristics_keys.push_back(
678 ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
679 res = info.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS,
680 avail_characteristics_keys.data(),
681 avail_characteristics_keys.size());
682 if (res != android::OK) {
683 return res;
684 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700685
686 *out = info.release();
687 return 0;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700688}
689
690void V4L2Camera::initDeviceInfo(camera_info_t* info) {
691 HAL_LOG_ENTER();
692
693 // For now, just constants.
694 info->facing = CAMERA_FACING_EXTERNAL;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700695 info->orientation = mOrientation;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700696 info->resource_cost = 100;
697 info->conflicting_devices = nullptr;
698 info->conflicting_devices_length = 0;
699}
700
701int V4L2Camera::initDevice() {
702 HAL_LOG_ENTER();
703
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700704 // Templates should be set up if they haven't already been.
705 if (!mTemplatesInitialized) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700706 int res = initTemplates();
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700707 if (res) {
708 return res;
709 }
710 }
711
712 return 0;
713}
714
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700715int V4L2Camera::enqueueBuffer(const camera3_stream_buffer_t* camera_buffer) {
716 HAL_LOG_ENTER();
717
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700718 int res = mV4L2Device->EnqueueBuffer(camera_buffer);
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700719 if (res) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700720 HAL_LOGE("Device failed to enqueue buffer.");
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700721 return res;
722 }
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700723
724 // Turn on the stream.
725 // TODO(b/29334616): Lock around stream on/off access, only start stream
726 // if not already on. (For now, since it's synchronous, it will always be
727 // turned off before another call to this function).
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700728 res = mV4L2Device->StreamOn();
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700729 if (res) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700730 HAL_LOGE("Device failed to turn on stream.");
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700731 return res;
732 }
733
734 // TODO(b/29334616): Enqueueing and dequeueing should be separate worker
735 // threads, not in the same function.
736
737 // Dequeue the buffer.
738 v4l2_buffer result_buffer;
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700739 res = mV4L2Device->DequeueBuffer(&result_buffer);
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700740 if (res) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700741 HAL_LOGE("Device failed to dequeue buffer.");
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700742 return res;
743 }
744
745 // All done, cleanup.
746 // TODO(b/29334616): Lock around stream on/off access, only stop stream if
747 // buffer queue is empty (synchronously, there's only ever 1 buffer in the
748 // queue at a time, so this is safe).
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700749 res = mV4L2Device->StreamOff();
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700750 if (res) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700751 HAL_LOGE("Device failed to turn off stream.");
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700752 return res;
753 }
754
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700755 return 0;
756}
757
758int V4L2Camera::getResultSettings(camera_metadata** metadata,
759 uint64_t* timestamp) {
760 HAL_LOG_ENTER();
761
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700762 int res = 0;
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700763 android::CameraMetadata frame_metadata(*metadata);
764
765 // TODO(b/30035628): fill in.
766 // For now just spoof the timestamp to a non-0 value and send it back.
767 int64_t frame_time = 1;
768 res = frame_metadata.update(ANDROID_SENSOR_TIMESTAMP, &frame_time, 1);
769 if (res != android::OK) {
770 return res;
771 }
772
773 *metadata = frame_metadata.release();
774 *timestamp = frame_time;
775
776 return 0;
777}
778
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700779int V4L2Camera::initTemplates() {
780 HAL_LOG_ENTER();
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700781 int res = 0;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700782
783 // Device characteristics need to be queried prior
784 // to template setup.
785 if (!mCharacteristicsInitialized) {
786 res = initCharacteristics();
787 if (res) {
788 return res;
789 }
790 }
791
792 // Note: static metadata expects all templates/requests
793 // to provide values for all supported keys.
794
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700795 android::CameraMetadata base_metadata;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700796
797 // Start with defaults for all templates.
798
799 /* android.colorCorrection. */
800
801 uint8_t aberration_mode = ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700802 res = base_metadata.update(ANDROID_COLOR_CORRECTION_ABERRATION_MODE,
803 &aberration_mode, 1);
804 if (res != android::OK) {
805 return res;
806 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700807
808 uint8_t color_correction_mode = ANDROID_COLOR_CORRECTION_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700809 res = base_metadata.update(ANDROID_COLOR_CORRECTION_MODE,
810 &color_correction_mode, 1);
811 if (res != android::OK) {
812 return res;
813 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700814
815 // transform and gains are for the unsupported MANUAL_POST_PROCESSING only.
816
817 /* android.control. */
818
819 /* AE. */
820 uint8_t ae_antibanding_mode = ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700821 res = base_metadata.update(ANDROID_CONTROL_AE_ANTIBANDING_MODE,
822 &ae_antibanding_mode, 1);
823 if (res != android::OK) {
824 return res;
825 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700826
827 // Only matters if AE_MODE = OFF
828 int32_t ae_exposure_compensation = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700829 res = base_metadata.update(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION,
830 &ae_exposure_compensation, 1);
831 if (res != android::OK) {
832 return res;
833 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700834
835 uint8_t ae_lock = ANDROID_CONTROL_AE_LOCK_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700836 res = base_metadata.update(ANDROID_CONTROL_AE_LOCK, &ae_lock, 1);
837 if (res != android::OK) {
838 return res;
839 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700840
841 uint8_t ae_mode = ANDROID_CONTROL_AE_MODE_ON;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700842 res = base_metadata.update(ANDROID_CONTROL_AE_MODE, &ae_mode, 1);
843 if (res != android::OK) {
844 return res;
845 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700846
847 // AE regions not supported.
848
849 // FPS set per-template.
850
851 uint8_t ae_precapture_trigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700852 res = base_metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
853 &ae_precapture_trigger, 1);
854 if (res != android::OK) {
855 return res;
856 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700857
858 /* AF. */
859
860 // AF mode set per-template.
861
862 // AF regions not supported.
863
864 uint8_t af_trigger = ANDROID_CONTROL_AF_TRIGGER_IDLE;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700865 res = base_metadata.update(ANDROID_CONTROL_AF_TRIGGER, &af_trigger, 1);
866 if (res != android::OK) {
867 return res;
868 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700869
870 /* AWB. */
871
872 // Priority: auto > off > Whatever is available.
873 uint8_t default_awb_mode = mAwbModes[0];
874 if (std::count(mAwbModes.begin(), mAwbModes.end(),
875 ANDROID_CONTROL_AWB_MODE_AUTO)) {
876 default_awb_mode = ANDROID_CONTROL_AWB_MODE_AUTO;
877 } else if (std::count(mAwbModes.begin(), mAwbModes.end(),
878 ANDROID_CONTROL_AWB_MODE_OFF)) {
879 default_awb_mode = ANDROID_CONTROL_AWB_MODE_OFF;
880 }
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700881 res = base_metadata.update(ANDROID_CONTROL_AWB_MODE, &default_awb_mode, 1);
882 if (res != android::OK) {
883 return res;
884 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700885
886 // AWB regions not supported.
887
888 /* Other controls. */
889
890 uint8_t effect_mode = ANDROID_CONTROL_EFFECT_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700891 res = base_metadata.update(ANDROID_CONTROL_EFFECT_MODE, &effect_mode, 1);
892 if (res != android::OK) {
893 return res;
894 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700895
896 uint8_t control_mode = ANDROID_CONTROL_MODE_AUTO;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700897 res = base_metadata.update(ANDROID_CONTROL_MODE, &control_mode, 1);
898 if (res != android::OK) {
899 return res;
900 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700901
902 uint8_t scene_mode = ANDROID_CONTROL_SCENE_MODE_DISABLED;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700903 res = base_metadata.update(ANDROID_CONTROL_SCENE_MODE,
904 &scene_mode, 1);
905 if (res != android::OK) {
906 return res;
907 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700908
909 uint8_t video_stabilization = ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700910 res = base_metadata.update(ANDROID_CONTROL_VIDEO_STABILIZATION_MODE,
911 &video_stabilization, 1);
912 if (res != android::OK) {
913 return res;
914 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700915
916 // postRawSensitivityBoost: RAW not supported, leave null.
917
918 /* android.demosaic. */
919
920 // mode marked FUTURE.
921
922 /* android.edge. */
923
924 uint8_t edge_mode = ANDROID_EDGE_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700925 res = base_metadata.update(ANDROID_EDGE_MODE, &edge_mode, 1);
926 if (res != android::OK) {
927 return res;
928 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700929
930 // strength marked FUTURE.
931
932 /* android.flash. */
933
934 // firingPower, firingTime marked FUTURE.
935
936 uint8_t flash_mode = ANDROID_FLASH_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700937 res = base_metadata.update(ANDROID_FLASH_MODE, &flash_mode, 1);
938 if (res != android::OK) {
939 return res;
940 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700941
942 /* android.hotPixel. */
943
944 uint8_t hp_mode = ANDROID_HOT_PIXEL_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700945 res = base_metadata.update(ANDROID_HOT_PIXEL_MODE, &hp_mode, 1);
946 if (res != android::OK) {
947 return res;
948 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700949
950 /* android.jpeg. */
951
952 double gps_coords[] = {/*latitude*/0, /*longitude*/0, /*altitude*/0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700953 res = base_metadata.update(ANDROID_JPEG_GPS_COORDINATES, gps_coords, 3);
954 if (res != android::OK) {
955 return res;
956 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700957
958 uint8_t gps_processing_method[] = "none";
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700959 res = base_metadata.update(ANDROID_JPEG_GPS_PROCESSING_METHOD,
960 gps_processing_method,
961 ARRAY_SIZE(gps_processing_method));
962 if (res != android::OK) {
963 return res;
964 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700965
966 int64_t gps_timestamp = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700967 res = base_metadata.update(ANDROID_JPEG_GPS_TIMESTAMP, &gps_timestamp, 1);
968 if (res != android::OK) {
969 return res;
970 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700971
972 // JPEG orientation is relative to sensor orientation (mOrientation).
973 int32_t jpeg_orientation = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700974 res = base_metadata.update(ANDROID_JPEG_ORIENTATION, &jpeg_orientation, 1);
975 if (res != android::OK) {
976 return res;
977 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700978
979 // 1-100, larger is higher quality.
980 uint8_t jpeg_quality = 80;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700981 res = base_metadata.update(ANDROID_JPEG_QUALITY, &jpeg_quality, 1);
982 if (res != android::OK) {
983 return res;
984 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700985
986 // TODO(b/29580107): If thumbnail quality actually matters/can be adjusted,
987 // adjust this.
988 uint8_t thumbnail_quality = 80;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700989 res = base_metadata.update(ANDROID_JPEG_THUMBNAIL_QUALITY,
990 &thumbnail_quality, 1);
991 if (res != android::OK) {
992 return res;
993 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700994
995 // TODO(b/29580107): Choose a size matching the resolution.
996 int32_t thumbnail_size[] = {0, 0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700997 res = base_metadata.update(ANDROID_JPEG_THUMBNAIL_SIZE, thumbnail_size, 2);
998 if (res != android::OK) {
999 return res;
1000 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001001
1002 /* android.lens. */
1003
1004 // Fixed values.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001005 res = base_metadata.update(ANDROID_LENS_APERTURE, &mAperture, 1);
1006 if (res != android::OK) {
1007 return res;
1008 }
1009 res = base_metadata.update(ANDROID_LENS_FILTER_DENSITY, &mFilterDensity, 1);
1010 if (res != android::OK) {
1011 return res;
1012 }
1013 res = base_metadata.update(ANDROID_LENS_FOCAL_LENGTH, &mFocalLength, 1);
1014 if (res != android::OK) {
1015 return res;
1016 }
1017 res = base_metadata.update(ANDROID_LENS_FOCUS_DISTANCE, &mFocusDistance, 1);
1018 if (res != android::OK) {
1019 return res;
1020 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001021
1022 uint8_t optical_stabilization = ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001023 res = base_metadata.update(ANDROID_LENS_OPTICAL_STABILIZATION_MODE,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001024 &optical_stabilization, 1);
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001025 if (res != android::OK) {
1026 return res;
1027 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001028
1029 /* android.noiseReduction. */
1030
1031 uint8_t noise_reduction_mode = ANDROID_NOISE_REDUCTION_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001032 res = base_metadata.update(ANDROID_NOISE_REDUCTION_MODE,
1033 &noise_reduction_mode, 1);
1034 if (res != android::OK) {
1035 return res;
1036 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001037
1038 // strength marked FUTURE.
1039
1040 /* android.request. */
1041
1042 // Request Id unused by the HAL for now, and these are just
1043 // templates, so just fill it in with a dummy.
1044 int32_t id = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001045 res = base_metadata.update(ANDROID_REQUEST_ID, &id, 1);
1046 if (res != android::OK) {
1047 return res;
1048 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001049
1050 // metadataMode marked FUTURE.
1051
1052 /* android.scaler. */
1053
1054 // No cropping by default; use the full active array.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001055 res = base_metadata.update(ANDROID_SCALER_CROP_REGION, mPixelArraySize.data(),
1056 mPixelArraySize.size());
1057 if (res != android::OK) {
1058 return res;
1059 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001060
1061 /* android.sensor. */
1062
1063 // exposureTime, sensitivity, testPattern[Data,Mode] not supported.
1064
1065 // Ignored when AE is OFF.
1066 int64_t frame_duration = 33333333L; // 1/30 s.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001067 res = base_metadata.update(ANDROID_SENSOR_FRAME_DURATION, &frame_duration, 1);
1068 if (res != android::OK) {
1069 return res;
1070 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001071
1072 /* android.shading. */
1073
1074 uint8_t shading_mode = ANDROID_SHADING_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001075 res = base_metadata.update(ANDROID_SHADING_MODE, &shading_mode, 1);
1076 if (res != android::OK) {
1077 return res;
1078 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001079
1080 /* android.statistics. */
1081
1082 uint8_t face_detect_mode = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001083 res = base_metadata.update(ANDROID_STATISTICS_FACE_DETECT_MODE,
1084 &face_detect_mode, 1);
1085 if (res != android::OK) {
1086 return res;
1087 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001088
1089 // histogramMode, sharpnessMapMode marked FUTURE.
1090
1091 uint8_t hp_map_mode = ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001092 res = base_metadata.update(ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE,
1093 &hp_map_mode, 1);
1094 if (res != android::OK) {
1095 return res;
1096 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001097
1098 uint8_t lens_shading_map_mode = ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001099 res = base_metadata.update(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE,
1100 &lens_shading_map_mode, 1);
1101 if (res != android::OK) {
1102 return res;
1103 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001104
1105 /* android.tonemap. */
1106
1107 // Tonemap only required for MANUAL_POST_PROCESSING capability.
1108
1109 /* android.led. */
1110
1111 uint8_t transmit = ANDROID_LED_TRANSMIT_ON;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001112 res = base_metadata.update(ANDROID_LED_TRANSMIT, &transmit, 1);
1113 if (res != android::OK) {
1114 return res;
1115 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001116
1117 /* android.reprocess */
1118
1119 // Only needed for REPROCESS capability.
1120
1121
1122 /* Template variable values. */
1123
1124 // Find the FPS ranges "closest" to a desired range
1125 // (minimum abs distance from min to min and max to max).
1126 // Find both a fixed rate and a variable rate, for different purposes.
1127 std::array<int32_t, 2> desired_flat_fps_range = {{30, 30}};
1128 std::array<int32_t, 2> desired_variable_fps_range = {{5, 30}};
1129 std::array<int32_t, 2> flat_fps_range;
1130 std::array<int32_t, 2> variable_fps_range;
1131 int32_t best_flat_distance = std::numeric_limits<int32_t>::max();
1132 int32_t best_variable_distance = std::numeric_limits<int32_t>::max();
1133 size_t num_fps_ranges = mFpsRanges.num_arrays();
1134 for (size_t i = 0; i < num_fps_ranges; ++i) {
1135 const int32_t* range = mFpsRanges[i];
1136 // Variable fps.
1137 int32_t distance = std::abs(range[0] - desired_variable_fps_range[0]) +
1138 std::abs(range[1] - desired_variable_fps_range[1]);
1139 if (distance < best_variable_distance) {
1140 variable_fps_range[0] = range[0];
1141 variable_fps_range[1] = range[1];
1142 best_variable_distance = distance;
1143 }
1144 // Flat fps. Only do if range is actually flat.
1145 // Note at least one flat range is required,
1146 // so something will always be filled in.
1147 if (range[0] == range[1]) {
1148 distance = std::abs(range[0] - desired_flat_fps_range[0]) +
1149 std::abs(range[1] - desired_flat_fps_range[1]);
1150 if (distance < best_flat_distance) {
1151 flat_fps_range[0] = range[0];
1152 flat_fps_range[1] = range[1];
1153 best_flat_distance = distance;
1154 }
1155 }
1156 }
1157
1158 // Priority: continuous > auto > off > whatever is available.
1159 bool continuous_still_avail = std::count(
1160 mAfModes.begin(), mAfModes.end(),
1161 ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE);
1162 bool continuous_video_avail = std::count(
1163 mAfModes.begin(), mAfModes.end(),
1164 ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO);
1165 uint8_t non_continuous_af_mode = mAfModes[0];
1166 if (std::count(mAfModes.begin(), mAfModes.end(),
1167 ANDROID_CONTROL_AF_MODE_AUTO)) {
1168 non_continuous_af_mode = ANDROID_CONTROL_AF_MODE_AUTO;
1169 } else if (std::count(mAfModes.begin(), mAfModes.end(),
1170 ANDROID_CONTROL_AF_MODE_OFF)) {
1171 non_continuous_af_mode = ANDROID_CONTROL_AF_MODE_OFF;
1172 }
1173 uint8_t still_af_mode = continuous_still_avail ?
1174 ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE : non_continuous_af_mode;
1175 uint8_t video_af_mode = continuous_video_avail ?
1176 ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO : non_continuous_af_mode;
1177
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001178 for (uint8_t template_id = 1; template_id < CAMERA3_TEMPLATE_COUNT;
1179 ++template_id) {
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001180 // General differences/support.
1181 uint8_t intent;
1182 uint8_t af_mode;
1183 std::array<int32_t, 2> fps_range;
1184 switch(template_id) {
1185 case CAMERA3_TEMPLATE_PREVIEW:
1186 intent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW;
1187 af_mode = still_af_mode;
1188 fps_range = flat_fps_range;
1189 break;
1190 case CAMERA3_TEMPLATE_STILL_CAPTURE:
1191 intent = ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE;
1192 af_mode = still_af_mode;
1193 fps_range = variable_fps_range;
1194 break;
1195 case CAMERA3_TEMPLATE_VIDEO_RECORD:
1196 intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD;
1197 af_mode = video_af_mode;
1198 fps_range = flat_fps_range;
1199 break;
1200 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
1201 intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT;
1202 af_mode = video_af_mode;
1203 fps_range = flat_fps_range;
1204 break;
1205 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG: // Fall through.
1206 case CAMERA3_TEMPLATE_MANUAL: // Fall though.
1207 default:
1208 // Unsupported/unrecognized. Don't add this template; skip it.
1209 continue;
1210 }
1211
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001212 // Copy our base metadata and add the new items.
1213 android::CameraMetadata template_metadata(base_metadata);
1214 res = template_metadata.update(ANDROID_CONTROL_CAPTURE_INTENT, &intent, 1);
1215 if (res != android::OK) {
1216 return res;
1217 }
1218 res = template_metadata.update(ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
1219 fps_range.data(), fps_range.size());
1220 if (res != android::OK) {
1221 return res;
1222 }
1223 res = template_metadata.update(ANDROID_CONTROL_AF_MODE, &af_mode, 1);
1224 if (res != android::OK) {
1225 return res;
1226 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001227
1228 const camera_metadata_t* template_raw_metadata =
1229 template_metadata.getAndLock();
1230 res = setTemplate(template_id, template_raw_metadata);
1231 if (res != android::OK) {
1232 return res;
1233 }
1234 res = template_metadata.unlock(template_raw_metadata);
1235 if (res != android::OK) {
1236 return res;
1237 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001238 }
1239
1240 mTemplatesInitialized = true;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001241 return 0;
1242}
1243
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001244bool V4L2Camera::isSupportedStreamSet(default_camera_hal::Stream** streams,
1245 int count, uint32_t mode) {
1246 HAL_LOG_ENTER();
1247
1248 if (mode != CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE) {
1249 HAL_LOGE("Unsupported stream configuration mode: %d", mode);
1250 return false;
1251 }
1252
1253 // This should be checked by the caller, but put here as a sanity check.
1254 if (count < 1) {
1255 HAL_LOGE("Must request at least 1 stream");
1256 return false;
1257 }
1258
1259 // Count the number of streams of each type.
1260 int32_t num_input = 0;
1261 int32_t num_raw = 0;
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -07001262 int32_t num_stalling = 0;
1263 int32_t num_non_stalling = 0;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001264 for (int i = 0; i < count; ++i) {
1265 default_camera_hal::Stream* stream = streams[i];
1266
1267 if (stream->isInputType()) {
1268 ++num_input;
1269 }
1270
1271 if (stream->isOutputType()) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -07001272 StreamFormat format(*stream);
1273 switch (format.Category()) {
1274 case kFormatCategoryRaw:
1275 ++num_raw;
1276 case kFormatCategoryStalling:
1277 ++num_stalling;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001278 break;
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -07001279 case kFormatCategoryNonStalling:
1280 ++num_non_stalling;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001281 break;
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -07001282 case kFormatCategoryUnknown: // Fall through.
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001283 default:
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001284 HAL_LOGE("Unsupported format for stream %d: %d", i, stream->getFormat());
1285 return false;
1286 }
1287 }
1288 }
1289
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -07001290 if (num_input > mMaxInputStreams ||
1291 num_raw > mMaxRawOutputStreams ||
1292 num_stalling > mMaxStallingOutputStreams ||
1293 num_non_stalling > mMaxNonStallingOutputStreams) {
1294 HAL_LOGE("Invalid stream configuration: %d input, %d RAW, %d stalling, "
1295 "%d non-stalling (max supported: %d input, %d RAW, %d stalling, "
1296 "%d non-stalling)", mMaxInputStreams, mMaxRawOutputStreams,
1297 mMaxStallingOutputStreams, mMaxNonStallingOutputStreams, num_input,
1298 num_raw, num_stalling, num_non_stalling);
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001299 return false;
1300 }
1301
1302 // TODO(b/29939583): The above logic should be all that's necessary,
1303 // but V4L2 doesn't actually support more than 1 stream at a time. So for now,
1304 // if not all streams are the same format and size, error. Note that this
1305 // means the HAL is not spec-compliant; the requested streams are technically
1306 // valid and it is not technically allowed to error once it has reached this
1307 // point.
1308 int format = streams[0]->getFormat();
1309 uint32_t width = streams[0]->getWidth();
1310 uint32_t height = streams[0]->getHeight();
1311 for (int i = 1; i < count; ++i) {
1312 const default_camera_hal::Stream* stream = streams[i];
1313 if (stream->getFormat() != format || stream->getWidth() != width ||
1314 stream->getHeight() != height) {
1315 HAL_LOGE("V4L2 only supports 1 stream configuration at a time "
1316 "(stream 0 is format %d, width %u, height %u, "
1317 "stream %d is format %d, width %u, height %u).",
1318 format, width, height, i, stream->getFormat(),
1319 stream->getWidth(), stream->getHeight());
1320 return false;
1321 }
1322 }
1323
1324 return true;
1325}
1326
1327int V4L2Camera::setupStream(default_camera_hal::Stream* stream,
1328 uint32_t* max_buffers) {
1329 HAL_LOG_ENTER();
1330
1331 if (stream->getRotation() != CAMERA3_STREAM_ROTATION_0) {
1332 HAL_LOGE("Rotation %d not supported", stream->getRotation());
1333 return -EINVAL;
1334 }
1335
1336 // Doesn't matter what was requested, we always use dataspace V0_JFIF.
1337 // Note: according to camera3.h, this isn't allowed, but etalvala@google.com
1338 // claims it's underdocumented; the implementation lets the HAL overwrite it.
1339 stream->setDataSpace(HAL_DATASPACE_V0_JFIF);
1340
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -07001341 int res = mV4L2Device->SetFormat(*stream, max_buffers);
1342 if (res) {
1343 HAL_LOGE("Failed to set device to correct format for stream.");
1344 return res;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001345 }
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001346 // Sanity check.
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -07001347 if (*max_buffers < 1) {
1348 HAL_LOGE("Setting format resulted in an invalid maximum of %u buffers.",
1349 *max_buffers);
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001350 return -ENODEV;
1351 }
1352
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001353 return 0;
1354}
1355
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001356bool V4L2Camera::isValidCaptureSettings(const camera_metadata_t* settings) {
1357 HAL_LOG_ENTER();
1358
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001359 // TODO(b/29335262): reject capture settings this camera isn't capable of.
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001360 return true;
1361}
1362
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001363int V4L2Camera::initCharacteristics() {
1364 HAL_LOG_ENTER();
1365
1366 /* Physical characteristics. */
1367 // No way to get these in V4L2, so faked.
1368 // Note: While many of these are primarily informative for post-processing
1369 // calculations by the app and will potentially cause bad results there,
1370 // focal length and physical size are actually used in framework
1371 // calculations (field of view, pixel pitch, etc), so faking them may
1372 // have unexpected results.
1373 mAperture = 2.0; // RPi camera v2 is f/2.0.
1374 mFilterDensity = 0.0;
1375 mFocalLength = 3.04; // RPi camera v2 is 3.04mm.
1376 mOrientation = 0;
1377 mPhysicalSize = {{3.674, 2.760}}; // RPi camera v2 is 3.674 x 2.760 mm.
1378
1379 /* Fixed features. */
1380
1381 // TODO(b/29394024): query VIDIOC_CROPCAP to get pixel rectangle.
1382 // Spoofing as 640 x 480 for now.
1383 mPixelArraySize = {{/*xmin*/0, /*ymin*/0, /*width*/640, /*height*/480}};
1384
1385 // V4L2 VIDIOC_CROPCAP doesn't give a way to query this;
1386 // it's driver dependent. For now, assume freeform, and
1387 // some cameras may just behave badly.
1388 // TODO(b/29579652): Figure out a way to determine this.
1389 mCropType = ANDROID_SCALER_CROPPING_TYPE_FREEFORM;
1390
1391 // TODO(b/29394024): query VIDIOC_CROPCAP to get cropping ranges,
1392 // and VIDIOC_G_CROP to determine if cropping is supported.
1393 // If the ioctl isn't available (or cropping has non-square pixelaspect),
1394 // assume no cropping/scaling.
1395 // May need to try setting some crops to determine what the driver actually
1396 // supports (including testing center vs freeform).
1397 mMaxZoom = 1;
1398
1399 // TODO(b/29394024): query V4L2_CID_EXPOSURE_BIAS.
1400 mAeCompensationRange = {{0, 0}};
1401 mAeCompensationStep = {1, 1};
1402
1403 // TODO(b/29394024): query V4L2_CID_3A_LOCK.
1404 mAeLockAvailable = ANDROID_CONTROL_AE_LOCK_AVAILABLE_FALSE;
1405 mAwbLockAvailable = ANDROID_CONTROL_AWB_LOCK_AVAILABLE_FALSE;
1406
1407 // TODO(b/29394024): query V4L2_CID_FLASH_LED_MODE.
1408 mFlashAvailable = 0;
1409
1410 // TODO(b/29394024): query V4L2_CID_FOCUS_ABSOLUTE for focus range.
1411 mFocusDistance = 0; // Fixed focus.
1412
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001413 // TODO(b/29939583): V4L2 can only support 1 stream at a time.
1414 // For now, just reporting minimum allowable for LIMITED devices.
1415 mMaxRawOutputStreams = 0;
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -07001416 mMaxStallingOutputStreams = 1;
1417 mMaxNonStallingOutputStreams = 2;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001418 // Reprocessing not supported.
1419 mMaxInputStreams = 0;
1420
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001421 /* Features with (potentially) multiple options. */
1422
1423 // TODO(b/29394024): query V4L2_CID_EXPOSURE_AUTO for ae modes.
1424 mAeModes.push_back(ANDROID_CONTROL_AE_MODE_ON);
1425
1426 // TODO(b/29394024): query V4L2_CID_POWER_LINE_FREQUENCY.
1427 // Auto as the default, since it could mean anything, while OFF would
1428 // require guaranteeing no antibanding happens.
1429 mAeAntibandingModes.push_back(ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO);
1430
1431 // TODO(b/29394024): query V4L2_CID_FOCUS_AUTO for
1432 // CONTINUOUS_VIDEO/CONTINUOUS_PICTURE. V4L2_CID_AUTO_FOCUS_START
1433 // supports what Android thinks of as auto focus (single auto focus).
1434 // V4L2_CID_AUTO_FOCUS_RANGE allows MACRO.
1435 mAfModes.push_back(ANDROID_CONTROL_AF_MODE_OFF);
1436
1437 // TODO(b/29394024): query V4L2_CID_AUTO_WHITE_BALANCE, or
1438 // V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE if available.
1439 mAwbModes.push_back(ANDROID_CONTROL_AWB_MODE_AUTO);
1440
1441 // TODO(b/29394024): query V4L2_CID_SCENE_MODE.
1442 mSceneModes.push_back(ANDROID_CONTROL_SCENE_MODE_DISABLED);
1443
1444 mControlModes.push_back(ANDROID_CONTROL_MODE_AUTO);
1445 if (mSceneModes.size() > 1) {
1446 // We have some mode other than just DISABLED available.
1447 mControlModes.push_back(ANDROID_CONTROL_MODE_USE_SCENE_MODE);
1448 }
1449
1450 // TODO(b/29394024): query V4L2_CID_COLORFX.
1451 mEffects.push_back(ANDROID_CONTROL_EFFECT_MODE_OFF);
1452
1453 // TODO(b/29394024): query V4L2_CID_FLASH_INDICATOR_INTENSITY.
1454 // For now, no indicator LED available; nothing to push back.
1455 // When there is, push back ANDROID_LED_AVAILABLE_LEDS_TRANSMIT.
1456
1457 // TODO(b/29394024): query V4L2_CID_IMAGE_STABILIZATION.
1458 mOpticalStabilizationModes.push_back(
1459 ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF);
1460 mVideoStabilizationModes.push_back(
1461 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF);
1462
1463 // Need to support YUV_420_888 and JPEG.
1464 // TODO(b/29394024): query VIDIOC_ENUM_FMT.
1465 std::vector<uint32_t> formats = {{V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420}};
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001466 // We want to find the smallest maximum frame duration amongst formats.
1467 mMaxFrameDuration = std::numeric_limits<int64_t>::max();
1468 int64_t min_yuv_frame_duration = std::numeric_limits<int64_t>::max();
1469 for (auto format : formats) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -07001470 int32_t hal_format = StreamFormat::V4L2ToHalPixelFormat(format);
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001471 if (hal_format < 0) {
1472 // Unrecognized/unused format. Skip it.
1473 continue;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001474 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001475 // TODO(b/29394024): query VIDIOC_ENUM_FRAMESIZES.
1476 // For now, just 640x480.
1477 ArrayVector<int32_t, 2> frame_sizes;
1478 frame_sizes.push_back({{640, 480}});
1479 size_t num_frame_sizes = frame_sizes.num_arrays();
1480 for (size_t i = 0; i < num_frame_sizes; ++i) {
1481 const int32_t* frame_size = frame_sizes[i];
1482 mStreamConfigs.push_back({{hal_format, frame_size[0], frame_size[1],
1483 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT}});
1484
1485 // TODO(b/29394024): query VIDIOC_ENUM_FRAMEINTERVALS.
1486 // For now, using the emulator max value of .3 sec.
1487 int64_t max_frame_duration = 300000000;
1488 // For now, min value of 30 fps = 1/30 spf ~= 33333333 ns.
1489 // For whatever reason the goldfish/qcom cameras report this as
1490 // 33331760, so copying that.
1491 int64_t min_frame_duration = 33331760;
1492
1493 mMinFrameDurations.push_back(
1494 {{hal_format, frame_size[0], frame_size[1], min_frame_duration}});
1495
1496 // In theory max frame duration (min frame rate) should be consistent
1497 // between all formats, but we check and only advertise the smallest
1498 // available max duration just in case.
1499 if (max_frame_duration < mMaxFrameDuration) {
1500 mMaxFrameDuration = max_frame_duration;
1501 }
1502
1503 // We only care about min frame duration (max frame rate) for YUV.
1504 if (hal_format == HAL_PIXEL_FORMAT_YCbCr_420_888 &&
1505 min_frame_duration < min_yuv_frame_duration) {
1506 min_yuv_frame_duration = min_frame_duration;
1507 }
1508
1509 // Usually 0 for non-jpeg, non-zero for JPEG.
1510 // Randomly choosing absurd 1 sec for JPEG. Unsure what this breaks.
1511 int64_t stall_duration = 0;
1512 if (hal_format == HAL_PIXEL_FORMAT_BLOB) {
1513 stall_duration = 1000000000;
1514 }
1515 mStallDurations.push_back(
1516 {{hal_format, frame_size[0], frame_size[1], stall_duration}});
1517 }
1518 }
1519
1520 // This should be at minimum {mi, ma}, {ma, ma} where mi and ma
1521 // are min and max frame rates for YUV_420_888. Min should be at most 15.
1522 // Convert from frame durations measured in ns.
1523 int32_t min_yuv_fps = 1000000000 / mMaxFrameDuration;
1524 if (min_yuv_fps > 15) {
1525 return -ENODEV;
1526 }
1527 int32_t max_yuv_fps = 1000000000 / min_yuv_frame_duration;
1528 mFpsRanges.push_back({{min_yuv_fps, max_yuv_fps}});
1529 mFpsRanges.push_back({{max_yuv_fps, max_yuv_fps}});
1530 // Always advertise {30, 30} if max is even higher,
1531 // since this is what the default video requests use.
1532 if (max_yuv_fps > 30) {
1533 mFpsRanges.push_back({{30, 30}});
1534 }
1535
1536 mCharacteristicsInitialized = true;
1537 return 0;
1538}
1539
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -07001540} // namespace v4l2_camera_hal