blob: 87a7a31d5923e36c0e966d11df9804730a7321f4 [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-Cohen9e6fd982016-08-02 16:29:53 -070054 std::shared_ptr<V4L2Wrapper> v4l2_wrapper(V4L2Wrapper::NewV4L2Wrapper(path));
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -070055 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-Cohen9e6fd982016-08-02 16:29:53 -070063V4L2Camera::V4L2Camera(int id, std::shared_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-Cohen9e6fd982016-08-02 16:29:53 -070078 if (mConnection) {
79 HAL_LOGE("Already connected. Please disconnect and try again.");
80 return -EIO;
81 }
82
83 mConnection.reset(new V4L2Wrapper::Connection(mV4L2Device));
84 if (mConnection->status()) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -070085 HAL_LOGE("Failed to connect to device.");
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070086 return mConnection->status();
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070087 }
88
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070089 // TODO(b/29185945): confirm this is a supported device.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070090 // This is checked by the HAL, but the device at mDevicePath may
91 // not be the same one that was there when the HAL was loaded.
92 // (Alternatively, better hotplugging support may make this unecessary
93 // by disabling cameras that get disconnected and checking newly connected
94 // cameras, so connect() is never called on an unsupported camera)
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070095
96 // TODO(b/29158098): Inform service of any flashes that are no longer available
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070097 // because this camera is in use.
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070098 return 0;
99}
100
101void V4L2Camera::disconnect() {
102 HAL_LOG_ENTER();
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700103
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -0700104 mConnection.reset();
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700105
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700106 // TODO(b/29158098): Inform service of any flashes that are available again
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700107 // because this camera is no longer in use.
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -0700108}
109
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700110int V4L2Camera::initStaticInfo(camera_metadata_t** out) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700111 HAL_LOG_ENTER();
112
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700113 android::status_t res;
114 // Device characteristics need to be queried prior
115 // to static info setup.
116 if (!mCharacteristicsInitialized) {
117 res = initCharacteristics();
118 if (res) {
119 return res;
120 }
121 }
122
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700123 android::CameraMetadata info;
Ari Hausman-Cohen63f69822016-06-10 11:40:35 -0700124
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700125 // Static metadata characteristics from /system/media/camera/docs/docs.html.
126
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700127 /* android.colorCorrection. */
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700128
129 // No easy way to turn chromatic aberration correction OFF in v4l2,
130 // though this may be supportable via a collection of other user controls.
131 uint8_t avail_aberration_modes[] = {
132 ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST,
133 ANDROID_COLOR_CORRECTION_ABERRATION_MODE_HIGH_QUALITY};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700134 res = info.update(ANDROID_COLOR_CORRECTION_AVAILABLE_ABERRATION_MODES,
135 avail_aberration_modes, ARRAY_SIZE(avail_aberration_modes));
136 if (res != android::OK) {
137 return res;
138 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700139
140 /* android.control. */
141
142 /* 3As */
143
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700144 res = info.update(ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES,
145 mAeAntibandingModes.data(), mAeAntibandingModes.size());
146 if (res != android::OK) {
147 return res;
148 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700149
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700150 res = info.update(ANDROID_CONTROL_AE_AVAILABLE_MODES,
151 mAeModes.data(), mAeModes.size());
152 if (res != android::OK) {
153 return res;
154 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700155
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700156 // Flatten mFpsRanges.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700157 res = info.update(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES,
158 mFpsRanges.data(), mFpsRanges.total_num_elements());
159 if (res != android::OK) {
160 return res;
161 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700162
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700163 res = info.update(ANDROID_CONTROL_AE_COMPENSATION_RANGE,
164 mAeCompensationRange.data(), mAeCompensationRange.size());
165 if (res != android::OK) {
166 return res;
167 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700168
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700169 res = info.update(ANDROID_CONTROL_AE_COMPENSATION_STEP,
170 &mAeCompensationStep, 1);
171 if (res != android::OK) {
172 return res;
173 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700174
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700175 res = info.update(ANDROID_CONTROL_AF_AVAILABLE_MODES,
176 mAfModes.data(), mAfModes.size());
177 if (res != android::OK) {
178 return res;
179 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700180
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700181 res = info.update(ANDROID_CONTROL_AWB_AVAILABLE_MODES,
182 mAwbModes.data(), mAwbModes.size());
183 if (res != android::OK) {
184 return res;
185 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700186
187 // Couldn't find any V4L2 support for regions, though maybe it's out there.
188 int32_t max_regions[] = {/*AE*/ 0,/*AWB*/ 0,/*AF*/ 0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700189 res = info.update(ANDROID_CONTROL_MAX_REGIONS,
190 max_regions, ARRAY_SIZE(max_regions));
191 if (res != android::OK) {
192 return res;
193 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700194
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700195 res = info.update(ANDROID_CONTROL_AE_LOCK_AVAILABLE,
196 &mAeLockAvailable, 1);
197 if (res != android::OK) {
198 return res;
199 }
200 res = info.update(ANDROID_CONTROL_AWB_LOCK_AVAILABLE,
201 &mAwbLockAvailable, 1);
202 if (res != android::OK) {
203 return res;
204 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700205
206 /* Scene modes. */
207
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700208 res = info.update(ANDROID_CONTROL_AVAILABLE_SCENE_MODES,
209 mSceneModes.data(), mSceneModes.size());
210 if (res != android::OK) {
211 return res;
212 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700213
214 // A 3-tuple of AE, AWB, AF overrides for each scene mode.
215 // Ignored for DISABLED, FACE_PRIORITY and FACE_PRIORITY_LOW_LIGHT.
216 uint8_t scene_mode_overrides[] = {
217 /*SCENE_MODE_DISABLED*/ /*AE*/0, /*AW*/0, /*AF*/0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700218 res = info.update(ANDROID_CONTROL_SCENE_MODE_OVERRIDES,
219 scene_mode_overrides, ARRAY_SIZE(scene_mode_overrides));
220 if (res != android::OK) {
221 return res;
222 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700223
224 /* Top level 3A/Scenes switch. */
225
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700226 res = info.update(ANDROID_CONTROL_AVAILABLE_MODES,
227 mControlModes.data(), mControlModes.size());
228 if (res != android::OK) {
229 return res;
230 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700231
232 /* Other android.control configuration. */
233
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700234 res = info.update(ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES,
235 mVideoStabilizationModes.data(),
236 mVideoStabilizationModes.size());
237 if (res != android::OK) {
238 return res;
239 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700240
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700241 res = info.update(ANDROID_CONTROL_AVAILABLE_EFFECTS,
242 mEffects.data(), mEffects.size());
243 if (res != android::OK) {
244 return res;
245 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700246
247 // AVAILABLE_HIGH_SPEED_VIDEO_CONFIGURATIONS only necessary
248 // for devices supporting CONSTRAINED_HIGH_SPEED_VIDEO,
249 // which this HAL doesn't support.
250
251 // POST_RAW_SENSITIVITY_BOOST_RANGE only necessary
252 // for devices supporting RAW format outputs.
253
254 /* android.edge. */
255
256 // Not sure if V4L2 does or doesn't do this, but HAL documentation says
257 // all devices must support FAST, and FAST can be equivalent to OFF, so
258 // either way it's fine to list.
259 uint8_t avail_edge_modes[] = {
260 ANDROID_EDGE_MODE_FAST};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700261 res = info.update(ANDROID_EDGE_AVAILABLE_EDGE_MODES,
262 avail_edge_modes, ARRAY_SIZE(avail_edge_modes));
263 if (res != android::OK) {
264 return res;
265 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700266
267 /* android.flash. */
268
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700269 res = info.update(ANDROID_FLASH_INFO_AVAILABLE,
270 &mFlashAvailable, 1);
271 if (res != android::OK) {
272 return res;
273 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700274
275 // info.chargeDuration, color.Temperature, maxEnergy marked FUTURE.
276
277 /* android.hotPixel. */
278
279 // No known V4L2 hot pixel correction. But it might be happening,
280 // so we report FAST/HIGH_QUALITY.
281 uint8_t avail_hot_pixel_modes[] = {
282 ANDROID_HOT_PIXEL_MODE_FAST,
283 ANDROID_HOT_PIXEL_MODE_HIGH_QUALITY};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700284 res = info.update(ANDROID_HOT_PIXEL_AVAILABLE_HOT_PIXEL_MODES,
285 avail_hot_pixel_modes, ARRAY_SIZE(avail_hot_pixel_modes));
286 if (res != android::OK) {
287 return res;
288 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700289
290 /* android.jpeg. */
291
292 // For now, no thumbnails available (only [0,0], the "no thumbnail" size).
293 // TODO(b/29580107): Could end up with a mismatch between request & result,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700294 // since V4L2 doesn't actually allow for thumbnail size control.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700295 int32_t thumbnail_sizes[] = {
296 0, 0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700297 res = info.update(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES,
298 thumbnail_sizes, ARRAY_SIZE(thumbnail_sizes));
299 if (res != android::OK) {
300 return res;
301 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700302
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700303 // V4L2 can query this with VIDIOC_TRY_FMT (or VIDIOC_S_FMT if TRY
304 // isn't supported), reading the fmt.pix.sizeimage for the largest
305 // jpeg size. For now use a constant (defined in V4L2Gralloc.h).
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700306 int32_t max_jpeg_size = V4L2_MAX_JPEG_SIZE;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700307 res = info.update(ANDROID_JPEG_MAX_SIZE,
308 &max_jpeg_size, 1);
309 if (res != android::OK) {
310 return res;
311 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700312
313 /* android.lens. */
314
315 /* Misc. lens control. */
316
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700317 res = info.update(ANDROID_LENS_INFO_AVAILABLE_APERTURES,
318 &mAperture, 1);
319 if (res != android::OK) {
320 return res;
321 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700322
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700323 res = info.update(ANDROID_LENS_INFO_AVAILABLE_FILTER_DENSITIES,
324 &mFilterDensity, 1);
325 if (res != android::OK) {
326 return res;
327 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700328
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700329 res = info.update(ANDROID_LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION,
330 mOpticalStabilizationModes.data(),
331 mOpticalStabilizationModes.size());
332 if (res != android::OK) {
333 return res;
334 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700335
Ari Hausman-Cohenab3a0a42016-08-02 11:10:56 -0700336 // lens.info.shadingMapSize not required for non-full devices.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700337
338 // All V4L2 devices are considered to be external facing.
339 uint8_t facing = ANDROID_LENS_FACING_EXTERNAL;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700340 res = info.update(ANDROID_LENS_FACING, &facing, 1);
341 if (res != android::OK) {
342 return res;
343 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700344
345 /* Zoom/Focus. */
346
347 // No way to actually get the focal length in V4L2, but it's a required key,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700348 // so we just fake it.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700349 res = info.update(ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS,
350 &mFocalLength, 1);
351 if (res != android::OK) {
352 return res;
353 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700354
355 // V4L2 focal units do not correspond to a particular physical unit.
356 uint8_t focus_calibration =
357 ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION_UNCALIBRATED;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700358 res = info.update(ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION,
359 &focus_calibration, 1);
360 if (res != android::OK) {
361 return res;
362 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700363
364 // info.hyperfocalDistance not required for UNCALIBRATED.
365
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700366 res = info.update(ANDROID_LENS_INFO_MINIMUM_FOCUS_DISTANCE,
367 &mFocusDistance, 1);
368 if (res != android::OK) {
369 return res;
370 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700371
372 /* Depth. */
373
374 // DEPTH capability not supported by this HAL. Not implemented:
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700375 // poseRotation
376 // poseTranslation
377 // intrinsicCalibration
378 // radialDistortion
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700379
380 /* anroid.noise. */
381
382 // Unable to control noise reduction in V4L2 devices,
383 // but FAST is allowed to be the same as OFF.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700384 uint8_t avail_noise_reduction_modes[] = {ANDROID_NOISE_REDUCTION_MODE_FAST};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700385 res = info.update(ANDROID_NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES,
386 avail_noise_reduction_modes,
387 ARRAY_SIZE(avail_noise_reduction_modes));
388 if (res != android::OK) {
389 return res;
390 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700391
392 /* android.request. */
393
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700394 int32_t max_num_output_streams[] = {mMaxRawOutputStreams,
395 mMaxNonStallingOutputStreams,
396 mMaxStallingOutputStreams};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700397 res = info.update(ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS,
398 max_num_output_streams, ARRAY_SIZE(max_num_output_streams));
399 if (res != android::OK) {
400 return res;
401 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700402
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700403 res = info.update(ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS,
404 &mMaxInputStreams, 1);
405 if (res != android::OK) {
406 return res;
407 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700408
409 // No way to know for V4L2, so fake with max allowable latency.
410 // Doesn't mean much without per-frame controls.
411 uint8_t pipeline_max_depth = 4;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700412 res = info.update(ANDROID_REQUEST_PIPELINE_MAX_DEPTH,
413 &pipeline_max_depth, 1);
414 if (res != android::OK) {
415 return res;
416 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700417
418 // Partial results not supported; partialResultCount defaults to 1.
419
420 // Available capabilities & keys queried at very end of this method.
421
422 /* android.scaler. */
423
424 /* Cropping. */
425
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700426 res = info.update(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
427 &mMaxZoom, 1);
428 if (res != android::OK) {
429 return res;
430 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700431
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700432 res = info.update(ANDROID_SCALER_CROPPING_TYPE, &mCropType, 1);
433 if (res != android::OK) {
434 return res;
435 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700436
437 /* Streams. */
438
439 // availableInputOutputFormatsMap only required for reprocessing capability.
440
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700441 // Flatten mStreamConfigs.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700442 res = info.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
443 mStreamConfigs.data(),
444 mStreamConfigs.total_num_elements());
445 if (res != android::OK) {
446 return res;
447 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700448
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700449 // Flatten mMinFrameDurations.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700450 res = info.update(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS,
451 mMinFrameDurations.data(),
452 mMinFrameDurations.total_num_elements());
453 if (res != android::OK) {
454 return res;
455 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700456
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700457 // Flatten mStallDurations.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700458 res = info.update(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS,
459 mStallDurations.data(),
460 mStallDurations.total_num_elements());
461 if (res != android::OK) {
462 return res;
463 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700464
465 /* android.sensor. */
466
467 /* Sizes. */
468
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700469 res = info.update(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE,
470 mPixelArraySize.data(), mPixelArraySize.size());
471 if (res != android::OK) {
472 return res;
473 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700474 // No V4L2 way to differentiate active vs. inactive parts of the rectangle.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700475 res = info.update(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE,
476 mPixelArraySize.data(), mPixelArraySize.size());
477 if (res != android::OK) {
478 return res;
479 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700480
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700481 res = info.update(ANDROID_SENSOR_INFO_PHYSICAL_SIZE,
482 mPhysicalSize.data(), mPhysicalSize.size());
483 if (res != android::OK) {
484 return res;
485 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700486
487 /* Misc sensor information. */
488
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700489 res = info.update(ANDROID_SENSOR_INFO_MAX_FRAME_DURATION,
490 &mMaxFrameDuration, 1);
491 if (res != android::OK) {
492 return res;
493 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700494
495 // HAL uses BOOTTIME timestamps.
496 // TODO(b/29457051): make sure timestamps are consistent throughout the HAL.
497 uint8_t timestamp_source = ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700498 res = info.update(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE,
499 &timestamp_source, 1);
500 if (res != android::OK) {
501 return res;
502 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700503
504 // As in initDeviceInfo, no way to actually get orientation.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700505 res = info.update(ANDROID_SENSOR_ORIENTATION, &mOrientation, 1);
506 if (res != android::OK) {
507 return res;
508 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700509
510 // availableTestPatternModes just defaults to OFF, which is fine.
511
512 // info.exposureTimeRange, info.sensitivityRange:
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700513 // exposure/sensitivity manual control not supported.
514 // Could query V4L2_CID_ISO_SENSITIVITY to support sensitivity if desired.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700515
516 // info.whiteLevel, info.lensShadingApplied,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700517 // info.preCorrectionPixelArraySize, referenceIlluminant1/2,
518 // calibrationTransform1/2, colorTransform1/2, forwardMatrix1/2,
519 // blackLevelPattern, profileHueSatMapDimensions
520 // all only necessary for RAW.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700521
522 // baseGainFactor marked FUTURE.
523
524 // maxAnalogSensitivity optional for LIMITED device.
525
526 // opticalBlackRegions: No known way to get in V4L2, but not necessary.
527
528 // opaqueRawSize not necessary since RAW_OPAQUE format not supported.
529
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700530 /* android.shading */
531
532 // No known V4L2 lens shading. But it might be happening,
533 // so we report FAST/HIGH_QUALITY.
534 uint8_t avail_shading_modes[] = {
535 ANDROID_SHADING_MODE_FAST,
536 ANDROID_SHADING_MODE_HIGH_QUALITY};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700537 res = info.update(ANDROID_SHADING_AVAILABLE_MODES,
538 avail_shading_modes, ARRAY_SIZE(avail_shading_modes));
539 if (res != android::OK) {
540 return res;
541 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700542
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700543 /* android.statistics */
544
545 // Face detection not supported.
546 uint8_t avail_face_detect_modes[] = {
547 ANDROID_STATISTICS_FACE_DETECT_MODE_OFF};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700548 res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES,
549 avail_face_detect_modes,
550 ARRAY_SIZE(avail_face_detect_modes));
551 if (res != android::OK) {
552 return res;
553 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700554
555 int32_t max_face_count = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700556 res = info.update(ANDROID_STATISTICS_INFO_MAX_FACE_COUNT,
557 &max_face_count, 1);
558 if (res != android::OK) {
559 return res;
560 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700561
562 // info.histogramBucketCount, info.maxHistogramCount,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700563 // info.maxSharpnessMapValue, info.sharpnessMapSizemarked FUTURE.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700564
565 // ON only needs to be supported for RAW capable devices.
566 uint8_t avail_hot_pixel_map_modes[] = {
567 ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700568 res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_HOT_PIXEL_MAP_MODES,
569 avail_hot_pixel_map_modes,
570 ARRAY_SIZE(avail_hot_pixel_map_modes));
571 if (res != android::OK) {
572 return res;
573 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700574
575 // ON only needs to be supported for RAW capable devices.
576 uint8_t avail_lens_shading_map_modes[] = {
577 ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700578 res = info.update(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES,
579 avail_lens_shading_map_modes,
580 ARRAY_SIZE(avail_lens_shading_map_modes));
581 if (res != android::OK) {
582 return res;
583 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700584
585 /* android.tonemap. */
586
587 // tonemapping only required for MANUAL_POST_PROCESSING capability.
588
589 /* android.led. */
590
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700591 // May or may not have LEDs available.
592 if (!mLeds.empty()) {
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700593 res = info.update(ANDROID_LED_AVAILABLE_LEDS, mLeds.data(), mLeds.size());
594 if (res != android::OK) {
595 return res;
596 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700597 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700598
599 /* android.sync. */
600
601 // "LIMITED devices are strongly encouraged to use a non-negative value.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700602 // If UNKNOWN is used here then app developers do not have a way to know
603 // when sensor settings have been applied." - Unfortunately, V4L2 doesn't
604 // really help here either. Could even be that adjusting settings mid-stream
605 // blocks in V4L2, and should be avoided.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700606 int32_t max_latency = ANDROID_SYNC_MAX_LATENCY_UNKNOWN;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700607 res = info.update(ANDROID_SYNC_MAX_LATENCY,
608 &max_latency, 1);
609 if (res != android::OK) {
610 return res;
611 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700612
613 /* android.reprocess. */
614
615 // REPROCESSING not supported by this HAL.
616
617 /* android.depth. */
618
619 // DEPTH not supported by this HAL.
620
621 /* Capabilities and android.info. */
622
623 uint8_t hw_level = ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700624 res = info.update(ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL,
625 &hw_level, 1);
626 if (res != android::OK) {
627 return res;
628 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700629
630 uint8_t capabilities[] = {
631 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700632 res = info.update(ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
633 capabilities, ARRAY_SIZE(capabilities));
634 if (res != android::OK) {
635 return res;
636 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700637
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700638 // Scan a default request template for included request keys.
639 if (!mTemplatesInitialized) {
640 res = initTemplates();
641 if (res) {
642 return res;
643 }
644 }
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700645 const camera_metadata_t* preview_request = nullptr;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700646 // Search templates from the beginning for a supported one.
647 for (uint8_t template_id = 1; template_id < CAMERA3_TEMPLATE_COUNT;
648 ++template_id) {
649 preview_request = constructDefaultRequestSettings(template_id);
650 if (preview_request != nullptr) {
651 break;
652 }
653 }
654 if (preview_request == nullptr) {
655 HAL_LOGE("No valid templates, can't get request keys.");
656 return -ENODEV;
657 }
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700658 std::vector<int32_t> avail_request_keys = getMetadataKeys(preview_request);
659 res = info.update(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS,
660 avail_request_keys.data(), avail_request_keys.size());
661 if (res != android::OK) {
662 return res;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700663 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700664
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700665 // Result keys will be duplicated from the request, plus a few extras.
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700666 // TODO(b/30035628): additonal available result keys.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700667 std::vector<int32_t> avail_result_keys(avail_request_keys);
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700668 res = info.update(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS,
669 avail_result_keys.data(), avail_result_keys.size());
670 if (res != android::OK) {
671 return res;
672 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700673
674 // Last thing, once all the available characteristics have been added.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700675 const camera_metadata_t* static_characteristics = info.getAndLock();
676 std::vector<int32_t> avail_characteristics_keys =
677 getMetadataKeys(static_characteristics);
678 res = info.unlock(static_characteristics);
679 if (res != android::OK) {
680 return res;
681 }
682 avail_characteristics_keys.push_back(
683 ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
684 res = info.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS,
685 avail_characteristics_keys.data(),
686 avail_characteristics_keys.size());
687 if (res != android::OK) {
688 return res;
689 }
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700690
691 *out = info.release();
692 return 0;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700693}
694
695void V4L2Camera::initDeviceInfo(camera_info_t* info) {
696 HAL_LOG_ENTER();
697
698 // For now, just constants.
699 info->facing = CAMERA_FACING_EXTERNAL;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700700 info->orientation = mOrientation;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700701 info->resource_cost = 100;
702 info->conflicting_devices = nullptr;
703 info->conflicting_devices_length = 0;
704}
705
706int V4L2Camera::initDevice() {
707 HAL_LOG_ENTER();
708
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700709 // Templates should be set up if they haven't already been.
710 if (!mTemplatesInitialized) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700711 int res = initTemplates();
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700712 if (res) {
713 return res;
714 }
715 }
716
717 return 0;
718}
719
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700720int V4L2Camera::enqueueBuffer(const camera3_stream_buffer_t* camera_buffer) {
721 HAL_LOG_ENTER();
722
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700723 int res = mV4L2Device->EnqueueBuffer(camera_buffer);
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700724 if (res) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700725 HAL_LOGE("Device failed to enqueue buffer.");
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700726 return res;
727 }
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700728
729 // Turn on the stream.
730 // TODO(b/29334616): Lock around stream on/off access, only start stream
731 // if not already on. (For now, since it's synchronous, it will always be
732 // turned off before another call to this function).
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700733 res = mV4L2Device->StreamOn();
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700734 if (res) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700735 HAL_LOGE("Device failed to turn on stream.");
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700736 return res;
737 }
738
739 // TODO(b/29334616): Enqueueing and dequeueing should be separate worker
740 // threads, not in the same function.
741
742 // Dequeue the buffer.
743 v4l2_buffer result_buffer;
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700744 res = mV4L2Device->DequeueBuffer(&result_buffer);
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700745 if (res) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700746 HAL_LOGE("Device failed to dequeue buffer.");
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700747 return res;
748 }
749
750 // All done, cleanup.
751 // TODO(b/29334616): Lock around stream on/off access, only stop stream if
752 // buffer queue is empty (synchronously, there's only ever 1 buffer in the
753 // queue at a time, so this is safe).
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700754 res = mV4L2Device->StreamOff();
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700755 if (res) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700756 HAL_LOGE("Device failed to turn off stream.");
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700757 return res;
758 }
759
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700760 return 0;
761}
762
763int V4L2Camera::getResultSettings(camera_metadata** metadata,
764 uint64_t* timestamp) {
765 HAL_LOG_ENTER();
766
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700767 int res = 0;
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700768 android::CameraMetadata frame_metadata(*metadata);
769
770 // TODO(b/30035628): fill in.
771 // For now just spoof the timestamp to a non-0 value and send it back.
772 int64_t frame_time = 1;
773 res = frame_metadata.update(ANDROID_SENSOR_TIMESTAMP, &frame_time, 1);
774 if (res != android::OK) {
775 return res;
776 }
777
778 *metadata = frame_metadata.release();
779 *timestamp = frame_time;
780
781 return 0;
782}
783
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700784int V4L2Camera::initTemplates() {
785 HAL_LOG_ENTER();
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -0700786 int res = 0;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700787
788 // Device characteristics need to be queried prior
789 // to template setup.
790 if (!mCharacteristicsInitialized) {
791 res = initCharacteristics();
792 if (res) {
793 return res;
794 }
795 }
796
797 // Note: static metadata expects all templates/requests
798 // to provide values for all supported keys.
799
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700800 android::CameraMetadata base_metadata;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700801
802 // Start with defaults for all templates.
803
804 /* android.colorCorrection. */
805
806 uint8_t aberration_mode = ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700807 res = base_metadata.update(ANDROID_COLOR_CORRECTION_ABERRATION_MODE,
808 &aberration_mode, 1);
809 if (res != android::OK) {
810 return res;
811 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700812
813 uint8_t color_correction_mode = ANDROID_COLOR_CORRECTION_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700814 res = base_metadata.update(ANDROID_COLOR_CORRECTION_MODE,
815 &color_correction_mode, 1);
816 if (res != android::OK) {
817 return res;
818 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700819
820 // transform and gains are for the unsupported MANUAL_POST_PROCESSING only.
821
822 /* android.control. */
823
824 /* AE. */
825 uint8_t ae_antibanding_mode = ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700826 res = base_metadata.update(ANDROID_CONTROL_AE_ANTIBANDING_MODE,
827 &ae_antibanding_mode, 1);
828 if (res != android::OK) {
829 return res;
830 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700831
832 // Only matters if AE_MODE = OFF
833 int32_t ae_exposure_compensation = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700834 res = base_metadata.update(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION,
835 &ae_exposure_compensation, 1);
836 if (res != android::OK) {
837 return res;
838 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700839
840 uint8_t ae_lock = ANDROID_CONTROL_AE_LOCK_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700841 res = base_metadata.update(ANDROID_CONTROL_AE_LOCK, &ae_lock, 1);
842 if (res != android::OK) {
843 return res;
844 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700845
846 uint8_t ae_mode = ANDROID_CONTROL_AE_MODE_ON;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700847 res = base_metadata.update(ANDROID_CONTROL_AE_MODE, &ae_mode, 1);
848 if (res != android::OK) {
849 return res;
850 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700851
852 // AE regions not supported.
853
854 // FPS set per-template.
855
856 uint8_t ae_precapture_trigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700857 res = base_metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
858 &ae_precapture_trigger, 1);
859 if (res != android::OK) {
860 return res;
861 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700862
863 /* AF. */
864
865 // AF mode set per-template.
866
867 // AF regions not supported.
868
869 uint8_t af_trigger = ANDROID_CONTROL_AF_TRIGGER_IDLE;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700870 res = base_metadata.update(ANDROID_CONTROL_AF_TRIGGER, &af_trigger, 1);
871 if (res != android::OK) {
872 return res;
873 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700874
875 /* AWB. */
876
877 // Priority: auto > off > Whatever is available.
878 uint8_t default_awb_mode = mAwbModes[0];
879 if (std::count(mAwbModes.begin(), mAwbModes.end(),
880 ANDROID_CONTROL_AWB_MODE_AUTO)) {
881 default_awb_mode = ANDROID_CONTROL_AWB_MODE_AUTO;
882 } else if (std::count(mAwbModes.begin(), mAwbModes.end(),
883 ANDROID_CONTROL_AWB_MODE_OFF)) {
884 default_awb_mode = ANDROID_CONTROL_AWB_MODE_OFF;
885 }
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700886 res = base_metadata.update(ANDROID_CONTROL_AWB_MODE, &default_awb_mode, 1);
887 if (res != android::OK) {
888 return res;
889 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700890
891 // AWB regions not supported.
892
893 /* Other controls. */
894
895 uint8_t effect_mode = ANDROID_CONTROL_EFFECT_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700896 res = base_metadata.update(ANDROID_CONTROL_EFFECT_MODE, &effect_mode, 1);
897 if (res != android::OK) {
898 return res;
899 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700900
901 uint8_t control_mode = ANDROID_CONTROL_MODE_AUTO;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700902 res = base_metadata.update(ANDROID_CONTROL_MODE, &control_mode, 1);
903 if (res != android::OK) {
904 return res;
905 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700906
907 uint8_t scene_mode = ANDROID_CONTROL_SCENE_MODE_DISABLED;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700908 res = base_metadata.update(ANDROID_CONTROL_SCENE_MODE,
909 &scene_mode, 1);
910 if (res != android::OK) {
911 return res;
912 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700913
914 uint8_t video_stabilization = ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700915 res = base_metadata.update(ANDROID_CONTROL_VIDEO_STABILIZATION_MODE,
916 &video_stabilization, 1);
917 if (res != android::OK) {
918 return res;
919 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700920
921 // postRawSensitivityBoost: RAW not supported, leave null.
922
923 /* android.demosaic. */
924
925 // mode marked FUTURE.
926
927 /* android.edge. */
928
929 uint8_t edge_mode = ANDROID_EDGE_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700930 res = base_metadata.update(ANDROID_EDGE_MODE, &edge_mode, 1);
931 if (res != android::OK) {
932 return res;
933 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700934
935 // strength marked FUTURE.
936
937 /* android.flash. */
938
939 // firingPower, firingTime marked FUTURE.
940
941 uint8_t flash_mode = ANDROID_FLASH_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700942 res = base_metadata.update(ANDROID_FLASH_MODE, &flash_mode, 1);
943 if (res != android::OK) {
944 return res;
945 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700946
947 /* android.hotPixel. */
948
949 uint8_t hp_mode = ANDROID_HOT_PIXEL_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700950 res = base_metadata.update(ANDROID_HOT_PIXEL_MODE, &hp_mode, 1);
951 if (res != android::OK) {
952 return res;
953 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700954
955 /* android.jpeg. */
956
957 double gps_coords[] = {/*latitude*/0, /*longitude*/0, /*altitude*/0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700958 res = base_metadata.update(ANDROID_JPEG_GPS_COORDINATES, gps_coords, 3);
959 if (res != android::OK) {
960 return res;
961 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700962
963 uint8_t gps_processing_method[] = "none";
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700964 res = base_metadata.update(ANDROID_JPEG_GPS_PROCESSING_METHOD,
965 gps_processing_method,
966 ARRAY_SIZE(gps_processing_method));
967 if (res != android::OK) {
968 return res;
969 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700970
971 int64_t gps_timestamp = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700972 res = base_metadata.update(ANDROID_JPEG_GPS_TIMESTAMP, &gps_timestamp, 1);
973 if (res != android::OK) {
974 return res;
975 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700976
977 // JPEG orientation is relative to sensor orientation (mOrientation).
978 int32_t jpeg_orientation = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700979 res = base_metadata.update(ANDROID_JPEG_ORIENTATION, &jpeg_orientation, 1);
980 if (res != android::OK) {
981 return res;
982 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700983
984 // 1-100, larger is higher quality.
985 uint8_t jpeg_quality = 80;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700986 res = base_metadata.update(ANDROID_JPEG_QUALITY, &jpeg_quality, 1);
987 if (res != android::OK) {
988 return res;
989 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700990
991 // TODO(b/29580107): If thumbnail quality actually matters/can be adjusted,
992 // adjust this.
993 uint8_t thumbnail_quality = 80;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -0700994 res = base_metadata.update(ANDROID_JPEG_THUMBNAIL_QUALITY,
995 &thumbnail_quality, 1);
996 if (res != android::OK) {
997 return res;
998 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700999
1000 // TODO(b/29580107): Choose a size matching the resolution.
1001 int32_t thumbnail_size[] = {0, 0};
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001002 res = base_metadata.update(ANDROID_JPEG_THUMBNAIL_SIZE, thumbnail_size, 2);
1003 if (res != android::OK) {
1004 return res;
1005 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001006
1007 /* android.lens. */
1008
1009 // Fixed values.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001010 res = base_metadata.update(ANDROID_LENS_APERTURE, &mAperture, 1);
1011 if (res != android::OK) {
1012 return res;
1013 }
1014 res = base_metadata.update(ANDROID_LENS_FILTER_DENSITY, &mFilterDensity, 1);
1015 if (res != android::OK) {
1016 return res;
1017 }
1018 res = base_metadata.update(ANDROID_LENS_FOCAL_LENGTH, &mFocalLength, 1);
1019 if (res != android::OK) {
1020 return res;
1021 }
1022 res = base_metadata.update(ANDROID_LENS_FOCUS_DISTANCE, &mFocusDistance, 1);
1023 if (res != android::OK) {
1024 return res;
1025 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001026
1027 uint8_t optical_stabilization = ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001028 res = base_metadata.update(ANDROID_LENS_OPTICAL_STABILIZATION_MODE,
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001029 &optical_stabilization, 1);
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001030 if (res != android::OK) {
1031 return res;
1032 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001033
1034 /* android.noiseReduction. */
1035
1036 uint8_t noise_reduction_mode = ANDROID_NOISE_REDUCTION_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001037 res = base_metadata.update(ANDROID_NOISE_REDUCTION_MODE,
1038 &noise_reduction_mode, 1);
1039 if (res != android::OK) {
1040 return res;
1041 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001042
1043 // strength marked FUTURE.
1044
1045 /* android.request. */
1046
1047 // Request Id unused by the HAL for now, and these are just
1048 // templates, so just fill it in with a dummy.
1049 int32_t id = 0;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001050 res = base_metadata.update(ANDROID_REQUEST_ID, &id, 1);
1051 if (res != android::OK) {
1052 return res;
1053 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001054
1055 // metadataMode marked FUTURE.
1056
1057 /* android.scaler. */
1058
1059 // No cropping by default; use the full active array.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001060 res = base_metadata.update(ANDROID_SCALER_CROP_REGION, mPixelArraySize.data(),
1061 mPixelArraySize.size());
1062 if (res != android::OK) {
1063 return res;
1064 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001065
1066 /* android.sensor. */
1067
1068 // exposureTime, sensitivity, testPattern[Data,Mode] not supported.
1069
1070 // Ignored when AE is OFF.
1071 int64_t frame_duration = 33333333L; // 1/30 s.
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001072 res = base_metadata.update(ANDROID_SENSOR_FRAME_DURATION, &frame_duration, 1);
1073 if (res != android::OK) {
1074 return res;
1075 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001076
1077 /* android.shading. */
1078
1079 uint8_t shading_mode = ANDROID_SHADING_MODE_FAST;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001080 res = base_metadata.update(ANDROID_SHADING_MODE, &shading_mode, 1);
1081 if (res != android::OK) {
1082 return res;
1083 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001084
1085 /* android.statistics. */
1086
1087 uint8_t face_detect_mode = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001088 res = base_metadata.update(ANDROID_STATISTICS_FACE_DETECT_MODE,
1089 &face_detect_mode, 1);
1090 if (res != android::OK) {
1091 return res;
1092 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001093
1094 // histogramMode, sharpnessMapMode marked FUTURE.
1095
1096 uint8_t hp_map_mode = ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001097 res = base_metadata.update(ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE,
1098 &hp_map_mode, 1);
1099 if (res != android::OK) {
1100 return res;
1101 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001102
1103 uint8_t lens_shading_map_mode = ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001104 res = base_metadata.update(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE,
1105 &lens_shading_map_mode, 1);
1106 if (res != android::OK) {
1107 return res;
1108 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001109
1110 /* android.tonemap. */
1111
1112 // Tonemap only required for MANUAL_POST_PROCESSING capability.
1113
1114 /* android.led. */
1115
1116 uint8_t transmit = ANDROID_LED_TRANSMIT_ON;
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001117 res = base_metadata.update(ANDROID_LED_TRANSMIT, &transmit, 1);
1118 if (res != android::OK) {
1119 return res;
1120 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001121
1122 /* android.reprocess */
1123
1124 // Only needed for REPROCESS capability.
1125
1126
1127 /* Template variable values. */
1128
1129 // Find the FPS ranges "closest" to a desired range
1130 // (minimum abs distance from min to min and max to max).
1131 // Find both a fixed rate and a variable rate, for different purposes.
1132 std::array<int32_t, 2> desired_flat_fps_range = {{30, 30}};
1133 std::array<int32_t, 2> desired_variable_fps_range = {{5, 30}};
1134 std::array<int32_t, 2> flat_fps_range;
1135 std::array<int32_t, 2> variable_fps_range;
1136 int32_t best_flat_distance = std::numeric_limits<int32_t>::max();
1137 int32_t best_variable_distance = std::numeric_limits<int32_t>::max();
1138 size_t num_fps_ranges = mFpsRanges.num_arrays();
1139 for (size_t i = 0; i < num_fps_ranges; ++i) {
1140 const int32_t* range = mFpsRanges[i];
1141 // Variable fps.
1142 int32_t distance = std::abs(range[0] - desired_variable_fps_range[0]) +
1143 std::abs(range[1] - desired_variable_fps_range[1]);
1144 if (distance < best_variable_distance) {
1145 variable_fps_range[0] = range[0];
1146 variable_fps_range[1] = range[1];
1147 best_variable_distance = distance;
1148 }
1149 // Flat fps. Only do if range is actually flat.
1150 // Note at least one flat range is required,
1151 // so something will always be filled in.
1152 if (range[0] == range[1]) {
1153 distance = std::abs(range[0] - desired_flat_fps_range[0]) +
1154 std::abs(range[1] - desired_flat_fps_range[1]);
1155 if (distance < best_flat_distance) {
1156 flat_fps_range[0] = range[0];
1157 flat_fps_range[1] = range[1];
1158 best_flat_distance = distance;
1159 }
1160 }
1161 }
1162
1163 // Priority: continuous > auto > off > whatever is available.
1164 bool continuous_still_avail = std::count(
1165 mAfModes.begin(), mAfModes.end(),
1166 ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE);
1167 bool continuous_video_avail = std::count(
1168 mAfModes.begin(), mAfModes.end(),
1169 ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO);
1170 uint8_t non_continuous_af_mode = mAfModes[0];
1171 if (std::count(mAfModes.begin(), mAfModes.end(),
1172 ANDROID_CONTROL_AF_MODE_AUTO)) {
1173 non_continuous_af_mode = ANDROID_CONTROL_AF_MODE_AUTO;
1174 } else if (std::count(mAfModes.begin(), mAfModes.end(),
1175 ANDROID_CONTROL_AF_MODE_OFF)) {
1176 non_continuous_af_mode = ANDROID_CONTROL_AF_MODE_OFF;
1177 }
1178 uint8_t still_af_mode = continuous_still_avail ?
1179 ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE : non_continuous_af_mode;
1180 uint8_t video_af_mode = continuous_video_avail ?
1181 ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO : non_continuous_af_mode;
1182
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001183 for (uint8_t template_id = 1; template_id < CAMERA3_TEMPLATE_COUNT;
1184 ++template_id) {
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001185 // General differences/support.
1186 uint8_t intent;
1187 uint8_t af_mode;
1188 std::array<int32_t, 2> fps_range;
1189 switch(template_id) {
1190 case CAMERA3_TEMPLATE_PREVIEW:
1191 intent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW;
1192 af_mode = still_af_mode;
1193 fps_range = flat_fps_range;
1194 break;
1195 case CAMERA3_TEMPLATE_STILL_CAPTURE:
1196 intent = ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE;
1197 af_mode = still_af_mode;
1198 fps_range = variable_fps_range;
1199 break;
1200 case CAMERA3_TEMPLATE_VIDEO_RECORD:
1201 intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD;
1202 af_mode = video_af_mode;
1203 fps_range = flat_fps_range;
1204 break;
1205 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
1206 intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT;
1207 af_mode = video_af_mode;
1208 fps_range = flat_fps_range;
1209 break;
1210 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG: // Fall through.
1211 case CAMERA3_TEMPLATE_MANUAL: // Fall though.
1212 default:
1213 // Unsupported/unrecognized. Don't add this template; skip it.
1214 continue;
1215 }
1216
Ari Hausman-Cohendde80172016-07-01 16:20:36 -07001217 // Copy our base metadata and add the new items.
1218 android::CameraMetadata template_metadata(base_metadata);
1219 res = template_metadata.update(ANDROID_CONTROL_CAPTURE_INTENT, &intent, 1);
1220 if (res != android::OK) {
1221 return res;
1222 }
1223 res = template_metadata.update(ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
1224 fps_range.data(), fps_range.size());
1225 if (res != android::OK) {
1226 return res;
1227 }
1228 res = template_metadata.update(ANDROID_CONTROL_AF_MODE, &af_mode, 1);
1229 if (res != android::OK) {
1230 return res;
1231 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001232
1233 const camera_metadata_t* template_raw_metadata =
1234 template_metadata.getAndLock();
1235 res = setTemplate(template_id, template_raw_metadata);
1236 if (res != android::OK) {
1237 return res;
1238 }
1239 res = template_metadata.unlock(template_raw_metadata);
1240 if (res != android::OK) {
1241 return res;
1242 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001243 }
1244
1245 mTemplatesInitialized = true;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001246 return 0;
1247}
1248
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001249bool V4L2Camera::isSupportedStreamSet(default_camera_hal::Stream** streams,
1250 int count, uint32_t mode) {
1251 HAL_LOG_ENTER();
1252
1253 if (mode != CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE) {
1254 HAL_LOGE("Unsupported stream configuration mode: %d", mode);
1255 return false;
1256 }
1257
1258 // This should be checked by the caller, but put here as a sanity check.
1259 if (count < 1) {
1260 HAL_LOGE("Must request at least 1 stream");
1261 return false;
1262 }
1263
1264 // Count the number of streams of each type.
1265 int32_t num_input = 0;
1266 int32_t num_raw = 0;
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -07001267 int32_t num_stalling = 0;
1268 int32_t num_non_stalling = 0;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001269 for (int i = 0; i < count; ++i) {
1270 default_camera_hal::Stream* stream = streams[i];
1271
1272 if (stream->isInputType()) {
1273 ++num_input;
1274 }
1275
1276 if (stream->isOutputType()) {
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -07001277 StreamFormat format(*stream);
1278 switch (format.Category()) {
1279 case kFormatCategoryRaw:
1280 ++num_raw;
1281 case kFormatCategoryStalling:
1282 ++num_stalling;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001283 break;
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -07001284 case kFormatCategoryNonStalling:
1285 ++num_non_stalling;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001286 break;
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -07001287 case kFormatCategoryUnknown: // Fall through.
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001288 default:
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001289 HAL_LOGE("Unsupported format for stream %d: %d", i, stream->getFormat());
1290 return false;
1291 }
1292 }
1293 }
1294
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -07001295 if (num_input > mMaxInputStreams ||
1296 num_raw > mMaxRawOutputStreams ||
1297 num_stalling > mMaxStallingOutputStreams ||
1298 num_non_stalling > mMaxNonStallingOutputStreams) {
1299 HAL_LOGE("Invalid stream configuration: %d input, %d RAW, %d stalling, "
1300 "%d non-stalling (max supported: %d input, %d RAW, %d stalling, "
1301 "%d non-stalling)", mMaxInputStreams, mMaxRawOutputStreams,
1302 mMaxStallingOutputStreams, mMaxNonStallingOutputStreams, num_input,
1303 num_raw, num_stalling, num_non_stalling);
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001304 return false;
1305 }
1306
1307 // TODO(b/29939583): The above logic should be all that's necessary,
1308 // but V4L2 doesn't actually support more than 1 stream at a time. So for now,
1309 // if not all streams are the same format and size, error. Note that this
1310 // means the HAL is not spec-compliant; the requested streams are technically
1311 // valid and it is not technically allowed to error once it has reached this
1312 // point.
1313 int format = streams[0]->getFormat();
1314 uint32_t width = streams[0]->getWidth();
1315 uint32_t height = streams[0]->getHeight();
1316 for (int i = 1; i < count; ++i) {
1317 const default_camera_hal::Stream* stream = streams[i];
1318 if (stream->getFormat() != format || stream->getWidth() != width ||
1319 stream->getHeight() != height) {
1320 HAL_LOGE("V4L2 only supports 1 stream configuration at a time "
1321 "(stream 0 is format %d, width %u, height %u, "
1322 "stream %d is format %d, width %u, height %u).",
1323 format, width, height, i, stream->getFormat(),
1324 stream->getWidth(), stream->getHeight());
1325 return false;
1326 }
1327 }
1328
1329 return true;
1330}
1331
1332int V4L2Camera::setupStream(default_camera_hal::Stream* stream,
1333 uint32_t* max_buffers) {
1334 HAL_LOG_ENTER();
1335
1336 if (stream->getRotation() != CAMERA3_STREAM_ROTATION_0) {
1337 HAL_LOGE("Rotation %d not supported", stream->getRotation());
1338 return -EINVAL;
1339 }
1340
1341 // Doesn't matter what was requested, we always use dataspace V0_JFIF.
1342 // Note: according to camera3.h, this isn't allowed, but etalvala@google.com
1343 // claims it's underdocumented; the implementation lets the HAL overwrite it.
1344 stream->setDataSpace(HAL_DATASPACE_V0_JFIF);
1345
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -07001346 int res = mV4L2Device->SetFormat(*stream, max_buffers);
1347 if (res) {
1348 HAL_LOGE("Failed to set device to correct format for stream.");
1349 return res;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001350 }
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001351 // Sanity check.
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -07001352 if (*max_buffers < 1) {
1353 HAL_LOGE("Setting format resulted in an invalid maximum of %u buffers.",
1354 *max_buffers);
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001355 return -ENODEV;
1356 }
1357
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001358 return 0;
1359}
1360
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001361bool V4L2Camera::isValidCaptureSettings(const camera_metadata_t* settings) {
1362 HAL_LOG_ENTER();
1363
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001364 // TODO(b/29335262): reject capture settings this camera isn't capable of.
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001365 return true;
1366}
1367
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001368int V4L2Camera::initCharacteristics() {
1369 HAL_LOG_ENTER();
1370
1371 /* Physical characteristics. */
1372 // No way to get these in V4L2, so faked.
1373 // Note: While many of these are primarily informative for post-processing
1374 // calculations by the app and will potentially cause bad results there,
1375 // focal length and physical size are actually used in framework
1376 // calculations (field of view, pixel pitch, etc), so faking them may
1377 // have unexpected results.
1378 mAperture = 2.0; // RPi camera v2 is f/2.0.
1379 mFilterDensity = 0.0;
1380 mFocalLength = 3.04; // RPi camera v2 is 3.04mm.
1381 mOrientation = 0;
1382 mPhysicalSize = {{3.674, 2.760}}; // RPi camera v2 is 3.674 x 2.760 mm.
1383
1384 /* Fixed features. */
1385
1386 // TODO(b/29394024): query VIDIOC_CROPCAP to get pixel rectangle.
1387 // Spoofing as 640 x 480 for now.
1388 mPixelArraySize = {{/*xmin*/0, /*ymin*/0, /*width*/640, /*height*/480}};
1389
1390 // V4L2 VIDIOC_CROPCAP doesn't give a way to query this;
1391 // it's driver dependent. For now, assume freeform, and
1392 // some cameras may just behave badly.
1393 // TODO(b/29579652): Figure out a way to determine this.
1394 mCropType = ANDROID_SCALER_CROPPING_TYPE_FREEFORM;
1395
1396 // TODO(b/29394024): query VIDIOC_CROPCAP to get cropping ranges,
1397 // and VIDIOC_G_CROP to determine if cropping is supported.
1398 // If the ioctl isn't available (or cropping has non-square pixelaspect),
1399 // assume no cropping/scaling.
1400 // May need to try setting some crops to determine what the driver actually
1401 // supports (including testing center vs freeform).
1402 mMaxZoom = 1;
1403
1404 // TODO(b/29394024): query V4L2_CID_EXPOSURE_BIAS.
1405 mAeCompensationRange = {{0, 0}};
1406 mAeCompensationStep = {1, 1};
1407
1408 // TODO(b/29394024): query V4L2_CID_3A_LOCK.
1409 mAeLockAvailable = ANDROID_CONTROL_AE_LOCK_AVAILABLE_FALSE;
1410 mAwbLockAvailable = ANDROID_CONTROL_AWB_LOCK_AVAILABLE_FALSE;
1411
1412 // TODO(b/29394024): query V4L2_CID_FLASH_LED_MODE.
1413 mFlashAvailable = 0;
1414
1415 // TODO(b/29394024): query V4L2_CID_FOCUS_ABSOLUTE for focus range.
1416 mFocusDistance = 0; // Fixed focus.
1417
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001418 // TODO(b/29939583): V4L2 can only support 1 stream at a time.
1419 // For now, just reporting minimum allowable for LIMITED devices.
1420 mMaxRawOutputStreams = 0;
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -07001421 mMaxStallingOutputStreams = 1;
1422 mMaxNonStallingOutputStreams = 2;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001423 // Reprocessing not supported.
1424 mMaxInputStreams = 0;
1425
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001426 /* Features with (potentially) multiple options. */
1427
1428 // TODO(b/29394024): query V4L2_CID_EXPOSURE_AUTO for ae modes.
1429 mAeModes.push_back(ANDROID_CONTROL_AE_MODE_ON);
1430
1431 // TODO(b/29394024): query V4L2_CID_POWER_LINE_FREQUENCY.
1432 // Auto as the default, since it could mean anything, while OFF would
1433 // require guaranteeing no antibanding happens.
1434 mAeAntibandingModes.push_back(ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO);
1435
1436 // TODO(b/29394024): query V4L2_CID_FOCUS_AUTO for
1437 // CONTINUOUS_VIDEO/CONTINUOUS_PICTURE. V4L2_CID_AUTO_FOCUS_START
1438 // supports what Android thinks of as auto focus (single auto focus).
1439 // V4L2_CID_AUTO_FOCUS_RANGE allows MACRO.
1440 mAfModes.push_back(ANDROID_CONTROL_AF_MODE_OFF);
1441
1442 // TODO(b/29394024): query V4L2_CID_AUTO_WHITE_BALANCE, or
1443 // V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE if available.
1444 mAwbModes.push_back(ANDROID_CONTROL_AWB_MODE_AUTO);
1445
1446 // TODO(b/29394024): query V4L2_CID_SCENE_MODE.
1447 mSceneModes.push_back(ANDROID_CONTROL_SCENE_MODE_DISABLED);
1448
1449 mControlModes.push_back(ANDROID_CONTROL_MODE_AUTO);
1450 if (mSceneModes.size() > 1) {
1451 // We have some mode other than just DISABLED available.
1452 mControlModes.push_back(ANDROID_CONTROL_MODE_USE_SCENE_MODE);
1453 }
1454
1455 // TODO(b/29394024): query V4L2_CID_COLORFX.
1456 mEffects.push_back(ANDROID_CONTROL_EFFECT_MODE_OFF);
1457
1458 // TODO(b/29394024): query V4L2_CID_FLASH_INDICATOR_INTENSITY.
1459 // For now, no indicator LED available; nothing to push back.
1460 // When there is, push back ANDROID_LED_AVAILABLE_LEDS_TRANSMIT.
1461
1462 // TODO(b/29394024): query V4L2_CID_IMAGE_STABILIZATION.
1463 mOpticalStabilizationModes.push_back(
1464 ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF);
1465 mVideoStabilizationModes.push_back(
1466 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF);
1467
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -07001468 // Need to be connected to query the device.
1469 V4L2Wrapper::Connection temp_connection(mV4L2Device);
1470 if (temp_connection.status()) {
1471 HAL_LOGE("Failed to connect to device.");
1472 return temp_connection.status();
1473 }
1474
1475 // Get all supported formats.
1476 std::set<uint32_t> v4l2_formats;
1477 int res = mV4L2Device->GetFormats(&v4l2_formats);
1478 if (res) {
1479 HAL_LOGE("Failed to get device formats.");
1480 return res;
1481 }
1482 std::set<int32_t> hal_formats;
1483 for (auto v4l2_format : v4l2_formats) {
1484 int32_t hal_format = StreamFormat::V4L2ToHalPixelFormat(v4l2_format);
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -07001485 if (hal_format < 0) {
1486 // Unrecognized/unused format. Skip it.
1487 continue;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001488 }
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -07001489 hal_formats.insert(hal_format);
1490 }
1491 // In addition to well-defined formats, must support "Implementation Defined"
1492 // (in this case what that means is managed by the StreamFormat class).
1493 hal_formats.insert(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED);
1494
1495 // Requirements check: need to support YCbCr_420_888 and JPEG.
1496 if (hal_formats.find(HAL_PIXEL_FORMAT_YCbCr_420_888) == hal_formats.end()) {
1497 HAL_LOGE("YCbCr_420_888 not supported by device.");
1498 return -ENODEV;
1499 } else if (hal_formats.find(HAL_PIXEL_FORMAT_BLOB) == hal_formats.end()) {
1500 HAL_LOGE("JPEG not supported by device.");
1501 return -ENODEV;
1502 }
1503
1504 // Find sizes and frame durations for all formats.
1505 // We also want to find the smallest max frame duration amongst all formats.
1506 mMaxFrameDuration = std::numeric_limits<int64_t>::max();
1507 int64_t min_yuv_frame_duration = std::numeric_limits<int64_t>::max();
1508 for (auto hal_format : hal_formats) {
1509 uint32_t v4l2_format = StreamFormat::HalToV4L2PixelFormat(hal_format);
1510 if (v4l2_format == 0) {
1511 // Unrecognized/unused format. Should never happen since hal_formats
1512 // came from translating a bunch of V4L2 formats above.
1513 HAL_LOGE("Couldn't find V4L2 format for HAL format %d", hal_format);
1514 return -ENODEV;
1515 }
1516
1517 std::set<std::array<int32_t, 2>> frame_sizes;
1518 res = mV4L2Device->GetFormatFrameSizes(v4l2_format, &frame_sizes);
1519 if (res) {
1520 HAL_LOGE("Failed to get all frame sizes for format %d", v4l2_format);
1521 return res;
1522 }
1523
1524 for (const auto& frame_size : frame_sizes) {
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001525 mStreamConfigs.push_back({{hal_format, frame_size[0], frame_size[1],
1526 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT}});
1527
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -07001528 std::array<int64_t, 2> duration_range;
1529 res = mV4L2Device->GetFormatFrameDurationRange(
1530 v4l2_format, frame_size, &duration_range);
1531 if (res) {
1532 HAL_LOGE("Failed to get frame duration range for format %d, "
1533 "size %u x %u", v4l2_format, frame_size[0], frame_size[1]);
1534 return res;
1535 }
1536
1537 int64_t min_frame_duration = duration_range[0];
1538 int64_t max_frame_duration = duration_range[1];
Ari Hausman-Cohen49925842016-06-21 14:07:58 -07001539
1540 mMinFrameDurations.push_back(
1541 {{hal_format, frame_size[0], frame_size[1], min_frame_duration}});
1542
1543 // In theory max frame duration (min frame rate) should be consistent
1544 // between all formats, but we check and only advertise the smallest
1545 // available max duration just in case.
1546 if (max_frame_duration < mMaxFrameDuration) {
1547 mMaxFrameDuration = max_frame_duration;
1548 }
1549
1550 // We only care about min frame duration (max frame rate) for YUV.
1551 if (hal_format == HAL_PIXEL_FORMAT_YCbCr_420_888 &&
1552 min_frame_duration < min_yuv_frame_duration) {
1553 min_yuv_frame_duration = min_frame_duration;
1554 }
1555
1556 // Usually 0 for non-jpeg, non-zero for JPEG.
1557 // Randomly choosing absurd 1 sec for JPEG. Unsure what this breaks.
1558 int64_t stall_duration = 0;
1559 if (hal_format == HAL_PIXEL_FORMAT_BLOB) {
1560 stall_duration = 1000000000;
1561 }
1562 mStallDurations.push_back(
1563 {{hal_format, frame_size[0], frame_size[1], stall_duration}});
1564 }
1565 }
1566
1567 // This should be at minimum {mi, ma}, {ma, ma} where mi and ma
1568 // are min and max frame rates for YUV_420_888. Min should be at most 15.
1569 // Convert from frame durations measured in ns.
1570 int32_t min_yuv_fps = 1000000000 / mMaxFrameDuration;
1571 if (min_yuv_fps > 15) {
1572 return -ENODEV;
1573 }
1574 int32_t max_yuv_fps = 1000000000 / min_yuv_frame_duration;
1575 mFpsRanges.push_back({{min_yuv_fps, max_yuv_fps}});
1576 mFpsRanges.push_back({{max_yuv_fps, max_yuv_fps}});
1577 // Always advertise {30, 30} if max is even higher,
1578 // since this is what the default video requests use.
1579 if (max_yuv_fps > 30) {
1580 mFpsRanges.push_back({{30, 30}});
1581 }
1582
1583 mCharacteristicsInitialized = true;
1584 return 0;
1585}
1586
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -07001587} // namespace v4l2_camera_hal