blob: 870cbcf160b9df2f22972ba1e51c41f06cf9af40 [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"
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -070031#include "StreamFormat.h"
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -070032#include "V4L2Gralloc.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
331 // No known V4L2 shading map info.
332 int32_t shading_map_size[] = {1, 1};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700333 res = info.update(ANDROID_LENS_INFO_SHADING_MAP_SIZE,
334 shading_map_size, ARRAY_SIZE(shading_map_size));
335 if (res != android::OK) {
336 return res;
337 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700338
339 // All V4L2 devices are considered to be external facing.
340 uint8_t facing = ANDROID_LENS_FACING_EXTERNAL;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700341 res = info.update(ANDROID_LENS_FACING, &facing, 1);
342 if (res != android::OK) {
343 return res;
344 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700345
346 /* Zoom/Focus. */
347
348 // No way to actually get the focal length in V4L2, but it's a required key,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700349 // so we just fake it.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700350 res = info.update(ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS,
351 &mFocalLength, 1);
352 if (res != android::OK) {
353 return res;
354 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700355
356 // V4L2 focal units do not correspond to a particular physical unit.
357 uint8_t focus_calibration =
358 ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION_UNCALIBRATED;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700359 res = info.update(ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION,
360 &focus_calibration, 1);
361 if (res != android::OK) {
362 return res;
363 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700364
365 // info.hyperfocalDistance not required for UNCALIBRATED.
366
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700367 res = info.update(ANDROID_LENS_INFO_MINIMUM_FOCUS_DISTANCE,
368 &mFocusDistance, 1);
369 if (res != android::OK) {
370 return res;
371 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700372
373 /* Depth. */
374
375 // DEPTH capability not supported by this HAL. Not implemented:
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700376 // poseRotation
377 // poseTranslation
378 // intrinsicCalibration
379 // radialDistortion
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700380
381 /* anroid.noise. */
382
383 // Unable to control noise reduction in V4L2 devices,
384 // but FAST is allowed to be the same as OFF.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700385 uint8_t avail_noise_reduction_modes[] = {ANDROID_NOISE_REDUCTION_MODE_FAST};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700386 res = info.update(ANDROID_NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES,
387 avail_noise_reduction_modes,
388 ARRAY_SIZE(avail_noise_reduction_modes));
389 if (res != android::OK) {
390 return res;
391 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700392
393 /* android.request. */
394
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700395 int32_t max_num_output_streams[] = {mMaxRawOutputStreams,
396 mMaxNonStallingOutputStreams,
397 mMaxStallingOutputStreams};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700398 res = info.update(ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS,
399 max_num_output_streams, ARRAY_SIZE(max_num_output_streams));
400 if (res != android::OK) {
401 return res;
402 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700403
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700404 res = info.update(ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS,
405 &mMaxInputStreams, 1);
406 if (res != android::OK) {
407 return res;
408 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700409
410 // No way to know for V4L2, so fake with max allowable latency.
411 // Doesn't mean much without per-frame controls.
412 uint8_t pipeline_max_depth = 4;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700413 res = info.update(ANDROID_REQUEST_PIPELINE_MAX_DEPTH,
414 &pipeline_max_depth, 1);
415 if (res != android::OK) {
416 return res;
417 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700418
419 // Partial results not supported; partialResultCount defaults to 1.
420
421 // Available capabilities & keys queried at very end of this method.
422
423 /* android.scaler. */
424
425 /* Cropping. */
426
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700427 res = info.update(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
428 &mMaxZoom, 1);
429 if (res != android::OK) {
430 return res;
431 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700432
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700433 res = info.update(ANDROID_SCALER_CROPPING_TYPE, &mCropType, 1);
434 if (res != android::OK) {
435 return res;
436 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700437
438 /* Streams. */
439
440 // availableInputOutputFormatsMap only required for reprocessing capability.
441
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700442 // Flatten mStreamConfigs.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700443 res = info.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
444 mStreamConfigs.data(),
445 mStreamConfigs.total_num_elements());
446 if (res != android::OK) {
447 return res;
448 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700449
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700450 // Flatten mMinFrameDurations.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700451 res = info.update(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS,
452 mMinFrameDurations.data(),
453 mMinFrameDurations.total_num_elements());
454 if (res != android::OK) {
455 return res;
456 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700457
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700458 // Flatten mStallDurations.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700459 res = info.update(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS,
460 mStallDurations.data(),
461 mStallDurations.total_num_elements());
462 if (res != android::OK) {
463 return res;
464 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700465
466 /* android.sensor. */
467
468 /* Sizes. */
469
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700470 res = info.update(ANDROID_SENSOR_INFO_PIXEL_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 // No V4L2 way to differentiate active vs. inactive parts of the rectangle.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700476 res = info.update(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE,
477 mPixelArraySize.data(), mPixelArraySize.size());
478 if (res != android::OK) {
479 return res;
480 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700481
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700482 res = info.update(ANDROID_SENSOR_INFO_PHYSICAL_SIZE,
483 mPhysicalSize.data(), mPhysicalSize.size());
484 if (res != android::OK) {
485 return res;
486 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700487
488 /* Misc sensor information. */
489
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700490 res = info.update(ANDROID_SENSOR_INFO_MAX_FRAME_DURATION,
491 &mMaxFrameDuration, 1);
492 if (res != android::OK) {
493 return res;
494 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700495
496 // HAL uses BOOTTIME timestamps.
497 // TODO(b/29457051): make sure timestamps are consistent throughout the HAL.
498 uint8_t timestamp_source = ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700499 res = info.update(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE,
500 &timestamp_source, 1);
501 if (res != android::OK) {
502 return res;
503 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700504
505 // As in initDeviceInfo, no way to actually get orientation.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700506 res = info.update(ANDROID_SENSOR_ORIENTATION, &mOrientation, 1);
507 if (res != android::OK) {
508 return res;
509 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700510
511 // availableTestPatternModes just defaults to OFF, which is fine.
512
513 // info.exposureTimeRange, info.sensitivityRange:
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700514 // exposure/sensitivity manual control not supported.
515 // Could query V4L2_CID_ISO_SENSITIVITY to support sensitivity if desired.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700516
517 // info.whiteLevel, info.lensShadingApplied,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700518 // info.preCorrectionPixelArraySize, referenceIlluminant1/2,
519 // calibrationTransform1/2, colorTransform1/2, forwardMatrix1/2,
520 // blackLevelPattern, profileHueSatMapDimensions
521 // all only necessary for RAW.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700522
523 // baseGainFactor marked FUTURE.
524
525 // maxAnalogSensitivity optional for LIMITED device.
526
527 // opticalBlackRegions: No known way to get in V4L2, but not necessary.
528
529 // opaqueRawSize not necessary since RAW_OPAQUE format not supported.
530
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700531 /* android.shading */
532
533 // No known V4L2 lens shading. But it might be happening,
534 // so we report FAST/HIGH_QUALITY.
535 uint8_t avail_shading_modes[] = {
536 ANDROID_SHADING_MODE_FAST,
537 ANDROID_SHADING_MODE_HIGH_QUALITY};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700538 res = info.update(ANDROID_SHADING_AVAILABLE_MODES,
539 avail_shading_modes, ARRAY_SIZE(avail_shading_modes));
540 if (res != android::OK) {
541 return res;
542 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700543
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700544 /* android.statistics */
545
546 // Face detection not supported.
547 uint8_t avail_face_detect_modes[] = {
548 ANDROID_STATISTICS_FACE_DETECT_MODE_OFF};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700549 res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES,
550 avail_face_detect_modes,
551 ARRAY_SIZE(avail_face_detect_modes));
552 if (res != android::OK) {
553 return res;
554 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700555
556 int32_t max_face_count = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700557 res = info.update(ANDROID_STATISTICS_INFO_MAX_FACE_COUNT,
558 &max_face_count, 1);
559 if (res != android::OK) {
560 return res;
561 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700562
563 // info.histogramBucketCount, info.maxHistogramCount,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700564 // info.maxSharpnessMapValue, info.sharpnessMapSizemarked FUTURE.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700565
566 // ON only needs to be supported for RAW capable devices.
567 uint8_t avail_hot_pixel_map_modes[] = {
568 ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700569 res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_HOT_PIXEL_MAP_MODES,
570 avail_hot_pixel_map_modes,
571 ARRAY_SIZE(avail_hot_pixel_map_modes));
572 if (res != android::OK) {
573 return res;
574 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700575
576 // ON only needs to be supported for RAW capable devices.
577 uint8_t avail_lens_shading_map_modes[] = {
578 ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700579 res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES,
580 avail_lens_shading_map_modes,
581 ARRAY_SIZE(avail_lens_shading_map_modes));
582 if (res != android::OK) {
583 return res;
584 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700585
586 /* android.tonemap. */
587
588 // tonemapping only required for MANUAL_POST_PROCESSING capability.
589
590 /* android.led. */
591
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700592 // May or may not have LEDs available.
593 if (!mLeds.empty()) {
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700594 res = info.update(ANDROID_LED_AVAILABLE_LEDS, mLeds.data(), mLeds.size());
595 if (res != android::OK) {
596 return res;
597 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700598 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700599
600 /* android.sync. */
601
602 // "LIMITED devices are strongly encouraged to use a non-negative value.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700603 // If UNKNOWN is used here then app developers do not have a way to know
604 // when sensor settings have been applied." - Unfortunately, V4L2 doesn't
605 // really help here either. Could even be that adjusting settings mid-stream
606 // blocks in V4L2, and should be avoided.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700607 int32_t max_latency = ANDROID_SYNC_MAX_LATENCY_UNKNOWN;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700608 res = info.update(ANDROID_SYNC_MAX_LATENCY,
609 &max_latency, 1);
610 if (res != android::OK) {
611 return res;
612 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700613
614 /* android.reprocess. */
615
616 // REPROCESSING not supported by this HAL.
617
618 /* android.depth. */
619
620 // DEPTH not supported by this HAL.
621
622 /* Capabilities and android.info. */
623
624 uint8_t hw_level = ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700625 res = info.update(ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL,
626 &hw_level, 1);
627 if (res != android::OK) {
628 return res;
629 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700630
631 uint8_t capabilities[] = {
632 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700633 res = info.update(ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
634 capabilities, ARRAY_SIZE(capabilities));
635 if (res != android::OK) {
636 return res;
637 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700638
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700639 // Scan a default request template for included request keys.
640 if (!mTemplatesInitialized) {
641 res = initTemplates();
642 if (res) {
643 return res;
644 }
645 }
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700646 const camera_metadata_t* preview_request = nullptr;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700647 // Search templates from the beginning for a supported one.
648 for (uint8_t template_id = 1; template_id < CAMERA3_TEMPLATE_COUNT;
649 ++template_id) {
650 preview_request = constructDefaultRequestSettings(template_id);
651 if (preview_request != nullptr) {
652 break;
653 }
654 }
655 if (preview_request == nullptr) {
656 HAL_LOGE("No valid templates, can't get request keys.");
657 return -ENODEV;
658 }
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700659 std::vector<int32_t> avail_request_keys = getMetadataKeys(preview_request);
660 res = info.update(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS,
661 avail_request_keys.data(), avail_request_keys.size());
662 if (res != android::OK) {
663 return res;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700664 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700665
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700666 // Result keys will be duplicated from the request, plus a few extras.
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700667 // TODO(b/30035628): additonal available result keys.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700668 std::vector<int32_t> avail_result_keys(avail_request_keys);
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700669 res = info.update(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS,
670 avail_result_keys.data(), avail_result_keys.size());
671 if (res != android::OK) {
672 return res;
673 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700674
675 // Last thing, once all the available characteristics have been added.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700676 const camera_metadata_t* static_characteristics = info.getAndLock();
677 std::vector<int32_t> avail_characteristics_keys =
678 getMetadataKeys(static_characteristics);
679 res = info.unlock(static_characteristics);
680 if (res != android::OK) {
681 return res;
682 }
683 avail_characteristics_keys.push_back(
684 ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
685 res = info.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS,
686 avail_characteristics_keys.data(),
687 avail_characteristics_keys.size());
688 if (res != android::OK) {
689 return res;
690 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700691
692 *out = info.release();
693 return 0;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700694}
695
696void V4L2Camera::initDeviceInfo(camera_info_t* info) {
697 HAL_LOG_ENTER();
698
699 // For now, just constants.
700 info->facing = CAMERA_FACING_EXTERNAL;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700701 info->orientation = mOrientation;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700702 info->resource_cost = 100;
703 info->conflicting_devices = nullptr;
704 info->conflicting_devices_length = 0;
705}
706
707int V4L2Camera::initDevice() {
708 HAL_LOG_ENTER();
709
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700710 // Templates should be set up if they haven't already been.
711 if (!mTemplatesInitialized) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700712 int res = initTemplates();
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700713 if (res) {
714 return res;
715 }
716 }
717
718 return 0;
719}
720
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700721int V4L2Camera::enqueueBuffer(const camera3_stream_buffer_t* camera_buffer) {
722 HAL_LOG_ENTER();
723
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700724 int res = mV4L2Device->EnqueueBuffer(camera_buffer);
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700725 if (res) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700726 HAL_LOGE("Device failed to enqueue buffer.");
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700727 return res;
728 }
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700729
730 // Turn on the stream.
731 // TODO(b/29334616): Lock around stream on/off access, only start stream
732 // if not already on. (For now, since it's synchronous, it will always be
733 // turned off before another call to this function).
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700734 res = mV4L2Device->StreamOn();
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700735 if (res) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700736 HAL_LOGE("Device failed to turn on stream.");
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700737 return res;
738 }
739
740 // TODO(b/29334616): Enqueueing and dequeueing should be separate worker
741 // threads, not in the same function.
742
743 // Dequeue the buffer.
744 v4l2_buffer result_buffer;
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700745 res = mV4L2Device->DequeueBuffer(&result_buffer);
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700746 if (res) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700747 HAL_LOGE("Device failed to dequeue buffer.");
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700748 return res;
749 }
750
751 // All done, cleanup.
752 // TODO(b/29334616): Lock around stream on/off access, only stop stream if
753 // buffer queue is empty (synchronously, there's only ever 1 buffer in the
754 // queue at a time, so this is safe).
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700755 res = mV4L2Device->StreamOff();
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700756 if (res) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700757 HAL_LOGE("Device failed to turn off stream.");
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700758 return res;
759 }
760
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700761 return 0;
762}
763
764int V4L2Camera::getResultSettings(camera_metadata** metadata,
765 uint64_t* timestamp) {
766 HAL_LOG_ENTER();
767
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700768 int res = 0;
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700769 android::CameraMetadata frame_metadata(*metadata);
770
771 // TODO(b/30035628): fill in.
772 // For now just spoof the timestamp to a non-0 value and send it back.
773 int64_t frame_time = 1;
774 res = frame_metadata.update(ANDROID_SENSOR_TIMESTAMP, &frame_time, 1);
775 if (res != android::OK) {
776 return res;
777 }
778
779 *metadata = frame_metadata.release();
780 *timestamp = frame_time;
781
782 return 0;
783}
784
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700785int V4L2Camera::initTemplates() {
786 HAL_LOG_ENTER();
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700787 int res = 0;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700788
789 // Device characteristics need to be queried prior
790 // to template setup.
791 if (!mCharacteristicsInitialized) {
792 res = initCharacteristics();
793 if (res) {
794 return res;
795 }
796 }
797
798 // Note: static metadata expects all templates/requests
799 // to provide values for all supported keys.
800
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700801 android::CameraMetadata base_metadata;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700802
803 // Start with defaults for all templates.
804
805 /* android.colorCorrection. */
806
807 uint8_t aberration_mode = ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700808 res = base_metadata.update(ANDROID_COLOR_CORRECTION_ABERRATION_MODE,
809 &aberration_mode, 1);
810 if (res != android::OK) {
811 return res;
812 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700813
814 uint8_t color_correction_mode = ANDROID_COLOR_CORRECTION_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700815 res = base_metadata.update(ANDROID_COLOR_CORRECTION_MODE,
816 &color_correction_mode, 1);
817 if (res != android::OK) {
818 return res;
819 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700820
821 // transform and gains are for the unsupported MANUAL_POST_PROCESSING only.
822
823 /* android.control. */
824
825 /* AE. */
826 uint8_t ae_antibanding_mode = ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700827 res = base_metadata.update(ANDROID_CONTROL_AE_ANTIBANDING_MODE,
828 &ae_antibanding_mode, 1);
829 if (res != android::OK) {
830 return res;
831 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700832
833 // Only matters if AE_MODE = OFF
834 int32_t ae_exposure_compensation = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700835 res = base_metadata.update(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION,
836 &ae_exposure_compensation, 1);
837 if (res != android::OK) {
838 return res;
839 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700840
841 uint8_t ae_lock = ANDROID_CONTROL_AE_LOCK_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700842 res = base_metadata.update(ANDROID_CONTROL_AE_LOCK, &ae_lock, 1);
843 if (res != android::OK) {
844 return res;
845 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700846
847 uint8_t ae_mode = ANDROID_CONTROL_AE_MODE_ON;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700848 res = base_metadata.update(ANDROID_CONTROL_AE_MODE, &ae_mode, 1);
849 if (res != android::OK) {
850 return res;
851 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700852
853 // AE regions not supported.
854
855 // FPS set per-template.
856
857 uint8_t ae_precapture_trigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700858 res = base_metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
859 &ae_precapture_trigger, 1);
860 if (res != android::OK) {
861 return res;
862 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700863
864 /* AF. */
865
866 // AF mode set per-template.
867
868 // AF regions not supported.
869
870 uint8_t af_trigger = ANDROID_CONTROL_AF_TRIGGER_IDLE;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700871 res = base_metadata.update(ANDROID_CONTROL_AF_TRIGGER, &af_trigger, 1);
872 if (res != android::OK) {
873 return res;
874 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700875
876 /* AWB. */
877
878 // Priority: auto > off > Whatever is available.
879 uint8_t default_awb_mode = mAwbModes[0];
880 if (std::count(mAwbModes.begin(), mAwbModes.end(),
881 ANDROID_CONTROL_AWB_MODE_AUTO)) {
882 default_awb_mode = ANDROID_CONTROL_AWB_MODE_AUTO;
883 } else if (std::count(mAwbModes.begin(), mAwbModes.end(),
884 ANDROID_CONTROL_AWB_MODE_OFF)) {
885 default_awb_mode = ANDROID_CONTROL_AWB_MODE_OFF;
886 }
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700887 res = base_metadata.update(ANDROID_CONTROL_AWB_MODE, &default_awb_mode, 1);
888 if (res != android::OK) {
889 return res;
890 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700891
892 // AWB regions not supported.
893
894 /* Other controls. */
895
896 uint8_t effect_mode = ANDROID_CONTROL_EFFECT_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700897 res = base_metadata.update(ANDROID_CONTROL_EFFECT_MODE, &effect_mode, 1);
898 if (res != android::OK) {
899 return res;
900 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700901
902 uint8_t control_mode = ANDROID_CONTROL_MODE_AUTO;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700903 res = base_metadata.update(ANDROID_CONTROL_MODE, &control_mode, 1);
904 if (res != android::OK) {
905 return res;
906 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700907
908 uint8_t scene_mode = ANDROID_CONTROL_SCENE_MODE_DISABLED;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700909 res = base_metadata.update(ANDROID_CONTROL_SCENE_MODE,
910 &scene_mode, 1);
911 if (res != android::OK) {
912 return res;
913 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700914
915 uint8_t video_stabilization = ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700916 res = base_metadata.update(ANDROID_CONTROL_VIDEO_STABILIZATION_MODE,
917 &video_stabilization, 1);
918 if (res != android::OK) {
919 return res;
920 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700921
922 // postRawSensitivityBoost: RAW not supported, leave null.
923
924 /* android.demosaic. */
925
926 // mode marked FUTURE.
927
928 /* android.edge. */
929
930 uint8_t edge_mode = ANDROID_EDGE_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700931 res = base_metadata.update(ANDROID_EDGE_MODE, &edge_mode, 1);
932 if (res != android::OK) {
933 return res;
934 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700935
936 // strength marked FUTURE.
937
938 /* android.flash. */
939
940 // firingPower, firingTime marked FUTURE.
941
942 uint8_t flash_mode = ANDROID_FLASH_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700943 res = base_metadata.update(ANDROID_FLASH_MODE, &flash_mode, 1);
944 if (res != android::OK) {
945 return res;
946 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700947
948 /* android.hotPixel. */
949
950 uint8_t hp_mode = ANDROID_HOT_PIXEL_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700951 res = base_metadata.update(ANDROID_HOT_PIXEL_MODE, &hp_mode, 1);
952 if (res != android::OK) {
953 return res;
954 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700955
956 /* android.jpeg. */
957
958 double gps_coords[] = {/*latitude*/0, /*longitude*/0, /*altitude*/0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700959 res = base_metadata.update(ANDROID_JPEG_GPS_COORDINATES, gps_coords, 3);
960 if (res != android::OK) {
961 return res;
962 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700963
964 uint8_t gps_processing_method[] = "none";
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700965 res = base_metadata.update(ANDROID_JPEG_GPS_PROCESSING_METHOD,
966 gps_processing_method,
967 ARRAY_SIZE(gps_processing_method));
968 if (res != android::OK) {
969 return res;
970 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700971
972 int64_t gps_timestamp = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700973 res = base_metadata.update(ANDROID_JPEG_GPS_TIMESTAMP, &gps_timestamp, 1);
974 if (res != android::OK) {
975 return res;
976 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700977
978 // JPEG orientation is relative to sensor orientation (mOrientation).
979 int32_t jpeg_orientation = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700980 res = base_metadata.update(ANDROID_JPEG_ORIENTATION, &jpeg_orientation, 1);
981 if (res != android::OK) {
982 return res;
983 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700984
985 // 1-100, larger is higher quality.
986 uint8_t jpeg_quality = 80;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700987 res = base_metadata.update(ANDROID_JPEG_QUALITY, &jpeg_quality, 1);
988 if (res != android::OK) {
989 return res;
990 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700991
992 // TODO(b/29580107): If thumbnail quality actually matters/can be adjusted,
993 // adjust this.
994 uint8_t thumbnail_quality = 80;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700995 res = base_metadata.update(ANDROID_JPEG_THUMBNAIL_QUALITY,
996 &thumbnail_quality, 1);
997 if (res != android::OK) {
998 return res;
999 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001000
1001 // TODO(b/29580107): Choose a size matching the resolution.
1002 int32_t thumbnail_size[] = {0, 0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001003 res = base_metadata.update(ANDROID_JPEG_THUMBNAIL_SIZE, thumbnail_size, 2);
1004 if (res != android::OK) {
1005 return res;
1006 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001007
1008 /* android.lens. */
1009
1010 // Fixed values.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001011 res = base_metadata.update(ANDROID_LENS_APERTURE, &mAperture, 1);
1012 if (res != android::OK) {
1013 return res;
1014 }
1015 res = base_metadata.update(ANDROID_LENS_FILTER_DENSITY, &mFilterDensity, 1);
1016 if (res != android::OK) {
1017 return res;
1018 }
1019 res = base_metadata.update(ANDROID_LENS_FOCAL_LENGTH, &mFocalLength, 1);
1020 if (res != android::OK) {
1021 return res;
1022 }
1023 res = base_metadata.update(ANDROID_LENS_FOCUS_DISTANCE, &mFocusDistance, 1);
1024 if (res != android::OK) {
1025 return res;
1026 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001027
1028 uint8_t optical_stabilization = ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001029 res = base_metadata.update(ANDROID_LENS_OPTICAL_STABILIZATION_MODE,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001030 &optical_stabilization, 1);
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001031 if (res != android::OK) {
1032 return res;
1033 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001034
1035 /* android.noiseReduction. */
1036
1037 uint8_t noise_reduction_mode = ANDROID_NOISE_REDUCTION_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001038 res = base_metadata.update(ANDROID_NOISE_REDUCTION_MODE,
1039 &noise_reduction_mode, 1);
1040 if (res != android::OK) {
1041 return res;
1042 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001043
1044 // strength marked FUTURE.
1045
1046 /* android.request. */
1047
1048 // Request Id unused by the HAL for now, and these are just
1049 // templates, so just fill it in with a dummy.
1050 int32_t id = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001051 res = base_metadata.update(ANDROID_REQUEST_ID, &id, 1);
1052 if (res != android::OK) {
1053 return res;
1054 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001055
1056 // metadataMode marked FUTURE.
1057
1058 /* android.scaler. */
1059
1060 // No cropping by default; use the full active array.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001061 res = base_metadata.update(ANDROID_SCALER_CROP_REGION, mPixelArraySize.data(),
1062 mPixelArraySize.size());
1063 if (res != android::OK) {
1064 return res;
1065 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001066
1067 /* android.sensor. */
1068
1069 // exposureTime, sensitivity, testPattern[Data,Mode] not supported.
1070
1071 // Ignored when AE is OFF.
1072 int64_t frame_duration = 33333333L; // 1/30 s.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001073 res = base_metadata.update(ANDROID_SENSOR_FRAME_DURATION, &frame_duration, 1);
1074 if (res != android::OK) {
1075 return res;
1076 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001077
1078 /* android.shading. */
1079
1080 uint8_t shading_mode = ANDROID_SHADING_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001081 res = base_metadata.update(ANDROID_SHADING_MODE, &shading_mode, 1);
1082 if (res != android::OK) {
1083 return res;
1084 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001085
1086 /* android.statistics. */
1087
1088 uint8_t face_detect_mode = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001089 res = base_metadata.update(ANDROID_STATISTICS_FACE_DETECT_MODE,
1090 &face_detect_mode, 1);
1091 if (res != android::OK) {
1092 return res;
1093 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001094
1095 // histogramMode, sharpnessMapMode marked FUTURE.
1096
1097 uint8_t hp_map_mode = ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001098 res = base_metadata.update(ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE,
1099 &hp_map_mode, 1);
1100 if (res != android::OK) {
1101 return res;
1102 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001103
1104 uint8_t lens_shading_map_mode = ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001105 res = base_metadata.update(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE,
1106 &lens_shading_map_mode, 1);
1107 if (res != android::OK) {
1108 return res;
1109 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001110
1111 /* android.tonemap. */
1112
1113 // Tonemap only required for MANUAL_POST_PROCESSING capability.
1114
1115 /* android.led. */
1116
1117 uint8_t transmit = ANDROID_LED_TRANSMIT_ON;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001118 res = base_metadata.update(ANDROID_LED_TRANSMIT, &transmit, 1);
1119 if (res != android::OK) {
1120 return res;
1121 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001122
1123 /* android.reprocess */
1124
1125 // Only needed for REPROCESS capability.
1126
1127
1128 /* Template variable values. */
1129
1130 // Find the FPS ranges "closest" to a desired range
1131 // (minimum abs distance from min to min and max to max).
1132 // Find both a fixed rate and a variable rate, for different purposes.
1133 std::array<int32_t, 2> desired_flat_fps_range = {{30, 30}};
1134 std::array<int32_t, 2> desired_variable_fps_range = {{5, 30}};
1135 std::array<int32_t, 2> flat_fps_range;
1136 std::array<int32_t, 2> variable_fps_range;
1137 int32_t best_flat_distance = std::numeric_limits<int32_t>::max();
1138 int32_t best_variable_distance = std::numeric_limits<int32_t>::max();
1139 size_t num_fps_ranges = mFpsRanges.num_arrays();
1140 for (size_t i = 0; i < num_fps_ranges; ++i) {
1141 const int32_t* range = mFpsRanges[i];
1142 // Variable fps.
1143 int32_t distance = std::abs(range[0] - desired_variable_fps_range[0]) +
1144 std::abs(range[1] - desired_variable_fps_range[1]);
1145 if (distance < best_variable_distance) {
1146 variable_fps_range[0] = range[0];
1147 variable_fps_range[1] = range[1];
1148 best_variable_distance = distance;
1149 }
1150 // Flat fps. Only do if range is actually flat.
1151 // Note at least one flat range is required,
1152 // so something will always be filled in.
1153 if (range[0] == range[1]) {
1154 distance = std::abs(range[0] - desired_flat_fps_range[0]) +
1155 std::abs(range[1] - desired_flat_fps_range[1]);
1156 if (distance < best_flat_distance) {
1157 flat_fps_range[0] = range[0];
1158 flat_fps_range[1] = range[1];
1159 best_flat_distance = distance;
1160 }
1161 }
1162 }
1163
1164 // Priority: continuous > auto > off > whatever is available.
1165 bool continuous_still_avail = std::count(
1166 mAfModes.begin(), mAfModes.end(),
1167 ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE);
1168 bool continuous_video_avail = std::count(
1169 mAfModes.begin(), mAfModes.end(),
1170 ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO);
1171 uint8_t non_continuous_af_mode = mAfModes[0];
1172 if (std::count(mAfModes.begin(), mAfModes.end(),
1173 ANDROID_CONTROL_AF_MODE_AUTO)) {
1174 non_continuous_af_mode = ANDROID_CONTROL_AF_MODE_AUTO;
1175 } else if (std::count(mAfModes.begin(), mAfModes.end(),
1176 ANDROID_CONTROL_AF_MODE_OFF)) {
1177 non_continuous_af_mode = ANDROID_CONTROL_AF_MODE_OFF;
1178 }
1179 uint8_t still_af_mode = continuous_still_avail ?
1180 ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE : non_continuous_af_mode;
1181 uint8_t video_af_mode = continuous_video_avail ?
1182 ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO : non_continuous_af_mode;
1183
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001184 for (uint8_t template_id = 1; template_id < CAMERA3_TEMPLATE_COUNT;
1185 ++template_id) {
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001186 // General differences/support.
1187 uint8_t intent;
1188 uint8_t af_mode;
1189 std::array<int32_t, 2> fps_range;
1190 switch(template_id) {
1191 case CAMERA3_TEMPLATE_PREVIEW:
1192 intent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW;
1193 af_mode = still_af_mode;
1194 fps_range = flat_fps_range;
1195 break;
1196 case CAMERA3_TEMPLATE_STILL_CAPTURE:
1197 intent = ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE;
1198 af_mode = still_af_mode;
1199 fps_range = variable_fps_range;
1200 break;
1201 case CAMERA3_TEMPLATE_VIDEO_RECORD:
1202 intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD;
1203 af_mode = video_af_mode;
1204 fps_range = flat_fps_range;
1205 break;
1206 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
1207 intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT;
1208 af_mode = video_af_mode;
1209 fps_range = flat_fps_range;
1210 break;
1211 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG: // Fall through.
1212 case CAMERA3_TEMPLATE_MANUAL: // Fall though.
1213 default:
1214 // Unsupported/unrecognized. Don't add this template; skip it.
1215 continue;
1216 }
1217
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001218 // Copy our base metadata and add the new items.
1219 android::CameraMetadata template_metadata(base_metadata);
1220 res = template_metadata.update(ANDROID_CONTROL_CAPTURE_INTENT, &intent, 1);
1221 if (res != android::OK) {
1222 return res;
1223 }
1224 res = template_metadata.update(ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
1225 fps_range.data(), fps_range.size());
1226 if (res != android::OK) {
1227 return res;
1228 }
1229 res = template_metadata.update(ANDROID_CONTROL_AF_MODE, &af_mode, 1);
1230 if (res != android::OK) {
1231 return res;
1232 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001233
1234 const camera_metadata_t* template_raw_metadata =
1235 template_metadata.getAndLock();
1236 res = setTemplate(template_id, template_raw_metadata);
1237 if (res != android::OK) {
1238 return res;
1239 }
1240 res = template_metadata.unlock(template_raw_metadata);
1241 if (res != android::OK) {
1242 return res;
1243 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001244 }
1245
1246 mTemplatesInitialized = true;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001247 return 0;
1248}
1249
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001250bool V4L2Camera::isSupportedStreamSet(default_camera_hal::Stream** streams,
1251 int count, uint32_t mode) {
1252 HAL_LOG_ENTER();
1253
1254 if (mode != CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE) {
1255 HAL_LOGE("Unsupported stream configuration mode: %d", mode);
1256 return false;
1257 }
1258
1259 // This should be checked by the caller, but put here as a sanity check.
1260 if (count < 1) {
1261 HAL_LOGE("Must request at least 1 stream");
1262 return false;
1263 }
1264
1265 // Count the number of streams of each type.
1266 int32_t num_input = 0;
1267 int32_t num_raw = 0;
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -07001268 int32_t num_stalling = 0;
1269 int32_t num_non_stalling = 0;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001270 for (int i = 0; i < count; ++i) {
1271 default_camera_hal::Stream* stream = streams[i];
1272
1273 if (stream->isInputType()) {
1274 ++num_input;
1275 }
1276
1277 if (stream->isOutputType()) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -07001278 StreamFormat format(*stream);
1279 switch (format.Category()) {
1280 case kFormatCategoryRaw:
1281 ++num_raw;
1282 case kFormatCategoryStalling:
1283 ++num_stalling;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001284 break;
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -07001285 case kFormatCategoryNonStalling:
1286 ++num_non_stalling;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001287 break;
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -07001288 case kFormatCategoryUnknown: // Fall through.
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001289 default:
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001290 HAL_LOGE("Unsupported format for stream %d: %d", i, stream->getFormat());
1291 return false;
1292 }
1293 }
1294 }
1295
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -07001296 if (num_input > mMaxInputStreams ||
1297 num_raw > mMaxRawOutputStreams ||
1298 num_stalling > mMaxStallingOutputStreams ||
1299 num_non_stalling > mMaxNonStallingOutputStreams) {
1300 HAL_LOGE("Invalid stream configuration: %d input, %d RAW, %d stalling, "
1301 "%d non-stalling (max supported: %d input, %d RAW, %d stalling, "
1302 "%d non-stalling)", mMaxInputStreams, mMaxRawOutputStreams,
1303 mMaxStallingOutputStreams, mMaxNonStallingOutputStreams, num_input,
1304 num_raw, num_stalling, num_non_stalling);
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001305 return false;
1306 }
1307
1308 // TODO(b/29939583): The above logic should be all that's necessary,
1309 // but V4L2 doesn't actually support more than 1 stream at a time. So for now,
1310 // if not all streams are the same format and size, error. Note that this
1311 // means the HAL is not spec-compliant; the requested streams are technically
1312 // valid and it is not technically allowed to error once it has reached this
1313 // point.
1314 int format = streams[0]->getFormat();
1315 uint32_t width = streams[0]->getWidth();
1316 uint32_t height = streams[0]->getHeight();
1317 for (int i = 1; i < count; ++i) {
1318 const default_camera_hal::Stream* stream = streams[i];
1319 if (stream->getFormat() != format || stream->getWidth() != width ||
1320 stream->getHeight() != height) {
1321 HAL_LOGE("V4L2 only supports 1 stream configuration at a time "
1322 "(stream 0 is format %d, width %u, height %u, "
1323 "stream %d is format %d, width %u, height %u).",
1324 format, width, height, i, stream->getFormat(),
1325 stream->getWidth(), stream->getHeight());
1326 return false;
1327 }
1328 }
1329
1330 return true;
1331}
1332
1333int V4L2Camera::setupStream(default_camera_hal::Stream* stream,
1334 uint32_t* max_buffers) {
1335 HAL_LOG_ENTER();
1336
1337 if (stream->getRotation() != CAMERA3_STREAM_ROTATION_0) {
1338 HAL_LOGE("Rotation %d not supported", stream->getRotation());
1339 return -EINVAL;
1340 }
1341
1342 // Doesn't matter what was requested, we always use dataspace V0_JFIF.
1343 // Note: according to camera3.h, this isn't allowed, but etalvala@google.com
1344 // claims it's underdocumented; the implementation lets the HAL overwrite it.
1345 stream->setDataSpace(HAL_DATASPACE_V0_JFIF);
1346
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -07001347 int res = mV4L2Device->SetFormat(*stream, max_buffers);
1348 if (res) {
1349 HAL_LOGE("Failed to set device to correct format for stream.");
1350 return res;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001351 }
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001352 // Sanity check.
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -07001353 if (*max_buffers < 1) {
1354 HAL_LOGE("Setting format resulted in an invalid maximum of %u buffers.",
1355 *max_buffers);
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001356 return -ENODEV;
1357 }
1358
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001359 return 0;
1360}
1361
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001362bool V4L2Camera::isValidCaptureSettings(const camera_metadata_t* settings) {
1363 HAL_LOG_ENTER();
1364
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001365 // TODO(b/29335262): reject capture settings this camera isn't capable of.
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001366 return true;
1367}
1368
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001369int V4L2Camera::initCharacteristics() {
1370 HAL_LOG_ENTER();
1371
1372 /* Physical characteristics. */
1373 // No way to get these in V4L2, so faked.
1374 // Note: While many of these are primarily informative for post-processing
1375 // calculations by the app and will potentially cause bad results there,
1376 // focal length and physical size are actually used in framework
1377 // calculations (field of view, pixel pitch, etc), so faking them may
1378 // have unexpected results.
1379 mAperture = 2.0; // RPi camera v2 is f/2.0.
1380 mFilterDensity = 0.0;
1381 mFocalLength = 3.04; // RPi camera v2 is 3.04mm.
1382 mOrientation = 0;
1383 mPhysicalSize = {{3.674, 2.760}}; // RPi camera v2 is 3.674 x 2.760 mm.
1384
1385 /* Fixed features. */
1386
1387 // TODO(b/29394024): query VIDIOC_CROPCAP to get pixel rectangle.
1388 // Spoofing as 640 x 480 for now.
1389 mPixelArraySize = {{/*xmin*/0, /*ymin*/0, /*width*/640, /*height*/480}};
1390
1391 // V4L2 VIDIOC_CROPCAP doesn't give a way to query this;
1392 // it's driver dependent. For now, assume freeform, and
1393 // some cameras may just behave badly.
1394 // TODO(b/29579652): Figure out a way to determine this.
1395 mCropType = ANDROID_SCALER_CROPPING_TYPE_FREEFORM;
1396
1397 // TODO(b/29394024): query VIDIOC_CROPCAP to get cropping ranges,
1398 // and VIDIOC_G_CROP to determine if cropping is supported.
1399 // If the ioctl isn't available (or cropping has non-square pixelaspect),
1400 // assume no cropping/scaling.
1401 // May need to try setting some crops to determine what the driver actually
1402 // supports (including testing center vs freeform).
1403 mMaxZoom = 1;
1404
1405 // TODO(b/29394024): query V4L2_CID_EXPOSURE_BIAS.
1406 mAeCompensationRange = {{0, 0}};
1407 mAeCompensationStep = {1, 1};
1408
1409 // TODO(b/29394024): query V4L2_CID_3A_LOCK.
1410 mAeLockAvailable = ANDROID_CONTROL_AE_LOCK_AVAILABLE_FALSE;
1411 mAwbLockAvailable = ANDROID_CONTROL_AWB_LOCK_AVAILABLE_FALSE;
1412
1413 // TODO(b/29394024): query V4L2_CID_FLASH_LED_MODE.
1414 mFlashAvailable = 0;
1415
1416 // TODO(b/29394024): query V4L2_CID_FOCUS_ABSOLUTE for focus range.
1417 mFocusDistance = 0; // Fixed focus.
1418
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001419 // TODO(b/29939583): V4L2 can only support 1 stream at a time.
1420 // For now, just reporting minimum allowable for LIMITED devices.
1421 mMaxRawOutputStreams = 0;
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -07001422 mMaxStallingOutputStreams = 1;
1423 mMaxNonStallingOutputStreams = 2;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001424 // Reprocessing not supported.
1425 mMaxInputStreams = 0;
1426
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001427 /* Features with (potentially) multiple options. */
1428
1429 // TODO(b/29394024): query V4L2_CID_EXPOSURE_AUTO for ae modes.
1430 mAeModes.push_back(ANDROID_CONTROL_AE_MODE_ON);
1431
1432 // TODO(b/29394024): query V4L2_CID_POWER_LINE_FREQUENCY.
1433 // Auto as the default, since it could mean anything, while OFF would
1434 // require guaranteeing no antibanding happens.
1435 mAeAntibandingModes.push_back(ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO);
1436
1437 // TODO(b/29394024): query V4L2_CID_FOCUS_AUTO for
1438 // CONTINUOUS_VIDEO/CONTINUOUS_PICTURE. V4L2_CID_AUTO_FOCUS_START
1439 // supports what Android thinks of as auto focus (single auto focus).
1440 // V4L2_CID_AUTO_FOCUS_RANGE allows MACRO.
1441 mAfModes.push_back(ANDROID_CONTROL_AF_MODE_OFF);
1442
1443 // TODO(b/29394024): query V4L2_CID_AUTO_WHITE_BALANCE, or
1444 // V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE if available.
1445 mAwbModes.push_back(ANDROID_CONTROL_AWB_MODE_AUTO);
1446
1447 // TODO(b/29394024): query V4L2_CID_SCENE_MODE.
1448 mSceneModes.push_back(ANDROID_CONTROL_SCENE_MODE_DISABLED);
1449
1450 mControlModes.push_back(ANDROID_CONTROL_MODE_AUTO);
1451 if (mSceneModes.size() > 1) {
1452 // We have some mode other than just DISABLED available.
1453 mControlModes.push_back(ANDROID_CONTROL_MODE_USE_SCENE_MODE);
1454 }
1455
1456 // TODO(b/29394024): query V4L2_CID_COLORFX.
1457 mEffects.push_back(ANDROID_CONTROL_EFFECT_MODE_OFF);
1458
1459 // TODO(b/29394024): query V4L2_CID_FLASH_INDICATOR_INTENSITY.
1460 // For now, no indicator LED available; nothing to push back.
1461 // When there is, push back ANDROID_LED_AVAILABLE_LEDS_TRANSMIT.
1462
1463 // TODO(b/29394024): query V4L2_CID_IMAGE_STABILIZATION.
1464 mOpticalStabilizationModes.push_back(
1465 ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF);
1466 mVideoStabilizationModes.push_back(
1467 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF);
1468
1469 // Need to support YUV_420_888 and JPEG.
1470 // TODO(b/29394024): query VIDIOC_ENUM_FMT.
1471 std::vector<uint32_t> formats = {{V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420}};
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001472 // We want to find the smallest maximum frame duration amongst formats.
1473 mMaxFrameDuration = std::numeric_limits<int64_t>::max();
1474 int64_t min_yuv_frame_duration = std::numeric_limits<int64_t>::max();
1475 for (auto format : formats) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -07001476 int32_t hal_format = StreamFormat::V4L2ToHalPixelFormat(format);
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001477 if (hal_format < 0) {
1478 // Unrecognized/unused format. Skip it.
1479 continue;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001480 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001481 // TODO(b/29394024): query VIDIOC_ENUM_FRAMESIZES.
1482 // For now, just 640x480.
1483 ArrayVector<int32_t, 2> frame_sizes;
1484 frame_sizes.push_back({{640, 480}});
1485 size_t num_frame_sizes = frame_sizes.num_arrays();
1486 for (size_t i = 0; i < num_frame_sizes; ++i) {
1487 const int32_t* frame_size = frame_sizes[i];
1488 mStreamConfigs.push_back({{hal_format, frame_size[0], frame_size[1],
1489 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT}});
1490
1491 // TODO(b/29394024): query VIDIOC_ENUM_FRAMEINTERVALS.
1492 // For now, using the emulator max value of .3 sec.
1493 int64_t max_frame_duration = 300000000;
1494 // For now, min value of 30 fps = 1/30 spf ~= 33333333 ns.
1495 // For whatever reason the goldfish/qcom cameras report this as
1496 // 33331760, so copying that.
1497 int64_t min_frame_duration = 33331760;
1498
1499 mMinFrameDurations.push_back(
1500 {{hal_format, frame_size[0], frame_size[1], min_frame_duration}});
1501
1502 // In theory max frame duration (min frame rate) should be consistent
1503 // between all formats, but we check and only advertise the smallest
1504 // available max duration just in case.
1505 if (max_frame_duration < mMaxFrameDuration) {
1506 mMaxFrameDuration = max_frame_duration;
1507 }
1508
1509 // We only care about min frame duration (max frame rate) for YUV.
1510 if (hal_format == HAL_PIXEL_FORMAT_YCbCr_420_888 &&
1511 min_frame_duration < min_yuv_frame_duration) {
1512 min_yuv_frame_duration = min_frame_duration;
1513 }
1514
1515 // Usually 0 for non-jpeg, non-zero for JPEG.
1516 // Randomly choosing absurd 1 sec for JPEG. Unsure what this breaks.
1517 int64_t stall_duration = 0;
1518 if (hal_format == HAL_PIXEL_FORMAT_BLOB) {
1519 stall_duration = 1000000000;
1520 }
1521 mStallDurations.push_back(
1522 {{hal_format, frame_size[0], frame_size[1], stall_duration}});
1523 }
1524 }
1525
1526 // This should be at minimum {mi, ma}, {ma, ma} where mi and ma
1527 // are min and max frame rates for YUV_420_888. Min should be at most 15.
1528 // Convert from frame durations measured in ns.
1529 int32_t min_yuv_fps = 1000000000 / mMaxFrameDuration;
1530 if (min_yuv_fps > 15) {
1531 return -ENODEV;
1532 }
1533 int32_t max_yuv_fps = 1000000000 / min_yuv_frame_duration;
1534 mFpsRanges.push_back({{min_yuv_fps, max_yuv_fps}});
1535 mFpsRanges.push_back({{max_yuv_fps, max_yuv_fps}});
1536 // Always advertise {30, 30} if max is even higher,
1537 // since this is what the default video requests use.
1538 if (max_yuv_fps > 30) {
1539 mFpsRanges.push_back({{30, 30}});
1540 }
1541
1542 mCharacteristicsInitialized = true;
1543 return 0;
1544}
1545
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001546} // namespace v4l2_camera_hal