• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "v4l2_metadata_factory.h"
18 
19 #include <camera/CameraMetadata.h>
20 
21 #include "common.h"
22 #include "format_metadata_factory.h"
23 #include "metadata/boottime_state_delegate.h"
24 #include "metadata/control.h"
25 #include "metadata/enum_converter.h"
26 #include "metadata/metadata_common.h"
27 #include "metadata/partial_metadata_factory.h"
28 #include "metadata/property.h"
29 #include "metadata/scaling_converter.h"
30 #include "v4l2_gralloc.h"
31 
32 namespace v4l2_camera_hal {
33 
34 // According to spec, each unit of V4L2_CID_AUTO_EXPOSURE_BIAS is 0.001 EV.
35 const camera_metadata_rational_t kAeCompensationUnit = {1, 1000};
36 // According to spec, each unit of V4L2_CID_EXPOSURE_ABSOLUTE is 100 us.
37 const int64_t kV4L2ExposureTimeStepNs = 100000;
38 // According to spec, each unit of V4L2_CID_ISO_SENSITIVITY is ISO/1000.
39 const int32_t kV4L2SensitivityDenominator = 1000;
40 
GetV4L2Metadata(std::shared_ptr<V4L2Wrapper> device,std::unique_ptr<Metadata> * result)41 int GetV4L2Metadata(std::shared_ptr<V4L2Wrapper> device,
42                     std::unique_ptr<Metadata>* result) {
43   HAL_LOG_ENTER();
44 
45   // Open a temporary connection to the device for all the V4L2 querying
46   // that will be happening (this could be done for each component individually,
47   // but doing it here prevents connecting and disconnecting for each one).
48   V4L2Wrapper::Connection temp_connection = V4L2Wrapper::Connection(device);
49   if (temp_connection.status()) {
50     HAL_LOGE("Failed to connect to device: %d.", temp_connection.status());
51     return temp_connection.status();
52   }
53 
54   // TODO(b/30035628): Add states.
55 
56   PartialMetadataSet components;
57 
58   components.insert(NoEffectMenuControl<uint8_t>(
59       ANDROID_COLOR_CORRECTION_ABERRATION_MODE,
60       ANDROID_COLOR_CORRECTION_AVAILABLE_ABERRATION_MODES,
61       {ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST,
62        ANDROID_COLOR_CORRECTION_ABERRATION_MODE_HIGH_QUALITY},
63       {{CAMERA3_TEMPLATE_STILL_CAPTURE,
64         ANDROID_COLOR_CORRECTION_ABERRATION_MODE_HIGH_QUALITY},
65        {OTHER_TEMPLATES, ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST}}));
66 
67   // TODO(b/30510395): subcomponents of 3A.
68   // In general, default to ON/AUTO since they imply pretty much nothing,
69   // while OFF implies guarantees about not hindering performance.
70   components.insert(std::unique_ptr<PartialMetadataInterface>(
71       new Property<std::array<int32_t, 3>>(ANDROID_CONTROL_MAX_REGIONS,
72                                            {{/*AE*/ 0, /*AWB*/ 0, /*AF*/ 0}})));
73   // TODO(b/30921166): V4L2_CID_AUTO_EXPOSURE_BIAS is an int menu, so
74   // this will be falling back to NoEffect until int menu support is added.
75   components.insert(V4L2ControlOrDefault<int32_t>(
76       ControlType::kSlider,
77       ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION,
78       ANDROID_CONTROL_AE_COMPENSATION_RANGE,
79       device,
80       V4L2_CID_AUTO_EXPOSURE_BIAS,
81       // No scaling necessary, AE_COMPENSATION_STEP handles this.
82       std::make_shared<ScalingConverter<int32_t, int32_t>>(1, 1),
83       0,
84       {{OTHER_TEMPLATES, 0}}));
85   components.insert(std::unique_ptr<PartialMetadataInterface>(
86       new Property<camera_metadata_rational_t>(
87           ANDROID_CONTROL_AE_COMPENSATION_STEP, kAeCompensationUnit)));
88   // TODO(b/31021522): Autofocus subcomponent.
89   components.insert(
90       NoEffectMenuControl<uint8_t>(ANDROID_CONTROL_AF_MODE,
91                                    ANDROID_CONTROL_AF_AVAILABLE_MODES,
92                                    {ANDROID_CONTROL_AF_MODE_OFF}));
93   // TODO(b/31021522): Should read autofocus state from
94   // V4L2_CID_AUTO_FOCUS_STATUS bitmask. The framework gets a little more
95   // complex than that does; there's a whole state-machine table in
96   // the docs (system/media/camera/docs/docs.html).
97   components.insert(FixedState<uint8_t>(ANDROID_CONTROL_AF_STATE,
98                                         ANDROID_CONTROL_AF_STATE_INACTIVE));
99   // TODO(b/31022735): Correctly implement AE & AF triggers that
100   // actually do something. These no effect triggers are even worse than most
101   // of the useless controls in this class, since technically they should
102   // revert back to IDLE eventually after START/CANCEL, but for now they won't
103   // unless IDLE is requested.
104   components.insert(
105       NoEffectMenuControl<uint8_t>(ANDROID_CONTROL_AF_TRIGGER,
106                                    DO_NOT_REPORT_OPTIONS,
107                                    {ANDROID_CONTROL_AF_TRIGGER_IDLE,
108                                     ANDROID_CONTROL_AF_TRIGGER_START,
109                                     ANDROID_CONTROL_AF_TRIGGER_CANCEL}));
110   components.insert(NoEffectMenuControl<uint8_t>(
111       ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
112       DO_NOT_REPORT_OPTIONS,
113       {ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE,
114        ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START,
115        ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL}));
116   components.insert(V4L2ControlOrDefault<uint8_t>(
117       ControlType::kMenu,
118       ANDROID_CONTROL_AE_ANTIBANDING_MODE,
119       ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES,
120       device,
121       V4L2_CID_POWER_LINE_FREQUENCY,
122       std::shared_ptr<ConverterInterface<uint8_t, int32_t>>(
123           new EnumConverter({{V4L2_CID_POWER_LINE_FREQUENCY_DISABLED,
124                               ANDROID_CONTROL_AE_ANTIBANDING_MODE_OFF},
125                              {V4L2_CID_POWER_LINE_FREQUENCY_50HZ,
126                               ANDROID_CONTROL_AE_ANTIBANDING_MODE_50HZ},
127                              {V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
128                               ANDROID_CONTROL_AE_ANTIBANDING_MODE_60HZ},
129                              {V4L2_CID_POWER_LINE_FREQUENCY_AUTO,
130                               ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO}})),
131       ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO,
132       {{CAMERA3_TEMPLATE_MANUAL, ANDROID_CONTROL_AE_ANTIBANDING_MODE_OFF},
133        {OTHER_TEMPLATES, ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO}}));
134   std::unique_ptr<PartialMetadataInterface> exposure_time =
135       V4L2Control<int64_t>(ControlType::kSlider,
136                            ANDROID_SENSOR_EXPOSURE_TIME,
137                            ANDROID_SENSOR_INFO_EXPOSURE_TIME_RANGE,
138                            device,
139                            V4L2_CID_EXPOSURE_ABSOLUTE,
140                            std::make_shared<ScalingConverter<int64_t, int32_t>>(
141                                kV4L2ExposureTimeStepNs, 1));
142   // TODO(b/31037072): Sensitivity has additional V4L2 controls
143   // (V4L2_CID_ISO_SENSITIVITY_AUTO), so this control currently has
144   // undefined behavior.
145   // TODO(b/30921166): V4L2_CID_ISO_SENSITIVITY is an int menu, so
146   // this will return nullptr until that is added.
147   std::unique_ptr<PartialMetadataInterface> sensitivity =
148       V4L2Control<int32_t>(ControlType::kSlider,
149                            ANDROID_SENSOR_SENSITIVITY,
150                            ANDROID_SENSOR_INFO_SENSITIVITY_RANGE,
151                            device,
152                            V4L2_CID_ISO_SENSITIVITY,
153                            std::make_shared<ScalingConverter<int32_t, int32_t>>(
154                                1, kV4L2SensitivityDenominator));
155   std::multimap<int32_t, uint8_t> ae_mode_mapping = {
156       {V4L2_EXPOSURE_AUTO, ANDROID_CONTROL_AE_MODE_ON}};
157   if (exposure_time && sensitivity) {
158     // TODO(b/30510395): as part of coordinated 3A component,
159     // if these aren't available don't advertise AE mode OFF, only AUTO.
160     components.insert(std::move(exposure_time));
161     components.insert(std::move(sensitivity));
162     ae_mode_mapping.emplace(V4L2_EXPOSURE_MANUAL, ANDROID_CONTROL_AE_MODE_OFF);
163   }
164   components.insert(V4L2ControlOrDefault<uint8_t>(
165       ControlType::kMenu,
166       ANDROID_CONTROL_AE_MODE,
167       ANDROID_CONTROL_AE_AVAILABLE_MODES,
168       device,
169       V4L2_CID_EXPOSURE_AUTO,
170       std::shared_ptr<ConverterInterface<uint8_t, int32_t>>(
171           new EnumConverter(ae_mode_mapping)),
172       ANDROID_CONTROL_AE_MODE_ON,
173       {{CAMERA3_TEMPLATE_MANUAL, ANDROID_CONTROL_AE_MODE_OFF},
174        {OTHER_TEMPLATES, ANDROID_CONTROL_AE_MODE_ON}}));
175   // Can't get AE status from V4L2.
176   // TODO(b/30510395): If AE mode is OFF, this should switch to INACTIVE.
177   components.insert(FixedState<uint8_t>(ANDROID_CONTROL_AE_STATE,
178                                         ANDROID_CONTROL_AE_STATE_CONVERGED));
179   // V4L2 offers multiple white balance interfaces. Try the advanced one before
180   // falling
181   // back to the simpler version.
182   // Modes from each API that don't match up:
183   // Android: WARM_FLUORESCENT, TWILIGHT.
184   // V4L2: FLUORESCENT_H, HORIZON, FLASH.
185   std::unique_ptr<PartialMetadataInterface> awb(V4L2Control<uint8_t>(
186       ControlType::kMenu,
187       ANDROID_CONTROL_AWB_MODE,
188       ANDROID_CONTROL_AWB_AVAILABLE_MODES,
189       device,
190       V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE,
191       std::shared_ptr<ConverterInterface<uint8_t, int32_t>>(new EnumConverter(
192           {{V4L2_WHITE_BALANCE_MANUAL, ANDROID_CONTROL_AWB_MODE_OFF},
193            {V4L2_WHITE_BALANCE_AUTO, ANDROID_CONTROL_AWB_MODE_AUTO},
194            {V4L2_WHITE_BALANCE_INCANDESCENT,
195             ANDROID_CONTROL_AWB_MODE_INCANDESCENT},
196            {V4L2_WHITE_BALANCE_FLUORESCENT,
197             ANDROID_CONTROL_AWB_MODE_FLUORESCENT},
198            {V4L2_WHITE_BALANCE_DAYLIGHT, ANDROID_CONTROL_AWB_MODE_DAYLIGHT},
199            {V4L2_WHITE_BALANCE_CLOUDY,
200             ANDROID_CONTROL_AWB_MODE_CLOUDY_DAYLIGHT},
201            {V4L2_WHITE_BALANCE_SHADE, ANDROID_CONTROL_AWB_MODE_SHADE}})),
202       {{CAMERA3_TEMPLATE_MANUAL, ANDROID_CONTROL_AWB_MODE_OFF},
203        {OTHER_TEMPLATES, ANDROID_CONTROL_AWB_MODE_AUTO}}));
204   if (awb) {
205     components.insert(std::move(awb));
206   } else {
207     // Fall back to simpler AWB or even just an ignored control.
208     components.insert(V4L2ControlOrDefault<uint8_t>(
209         ControlType::kMenu,
210         ANDROID_CONTROL_AWB_MODE,
211         ANDROID_CONTROL_AWB_AVAILABLE_MODES,
212         device,
213         V4L2_CID_AUTO_WHITE_BALANCE,
214         std::shared_ptr<ConverterInterface<uint8_t, int32_t>>(
215             new EnumConverter({{0, ANDROID_CONTROL_AWB_MODE_OFF},
216                                {1, ANDROID_CONTROL_AWB_MODE_AUTO}})),
217         ANDROID_CONTROL_AWB_MODE_AUTO,
218         {{CAMERA3_TEMPLATE_MANUAL, ANDROID_CONTROL_AWB_MODE_OFF},
219          {OTHER_TEMPLATES, ANDROID_CONTROL_AWB_MODE_AUTO}}));
220   }
221   // TODO(b/31041577): Handle AWB state machine correctly.
222   components.insert(FixedState<uint8_t>(ANDROID_CONTROL_AWB_STATE,
223                                         ANDROID_CONTROL_AWB_STATE_CONVERGED));
224   // TODO(b/31022153): 3A locks.
225   components.insert(std::unique_ptr<PartialMetadataInterface>(
226       new Property<uint8_t>(ANDROID_CONTROL_AE_LOCK_AVAILABLE,
227                             ANDROID_CONTROL_AE_LOCK_AVAILABLE_FALSE)));
228   components.insert(
229       NoEffectMenuControl<uint8_t>(ANDROID_CONTROL_AE_LOCK,
230                                    DO_NOT_REPORT_OPTIONS,
231                                    {ANDROID_CONTROL_AE_LOCK_OFF}));
232   components.insert(std::unique_ptr<PartialMetadataInterface>(
233       new Property<uint8_t>(ANDROID_CONTROL_AWB_LOCK_AVAILABLE,
234                             ANDROID_CONTROL_AWB_LOCK_AVAILABLE_FALSE)));
235   components.insert(
236       NoEffectMenuControl<uint8_t>(ANDROID_CONTROL_AWB_LOCK,
237                                    DO_NOT_REPORT_OPTIONS,
238                                    {ANDROID_CONTROL_AWB_LOCK_OFF}));
239   // TODO(b/30510395): subcomponents of scene modes
240   // (may itself be a subcomponent of 3A).
241   // Modes from each API that don't match up:
242   // Android: FACE_PRIORITY, ACTION, NIGHT_PORTRAIT, THEATRE, STEADYPHOTO,
243   // BARCODE, HIGH_SPEED_VIDEO.
244   // V4L2: BACKLIGHT, DAWN_DUSK, FALL_COLORS, TEXT.
245   components.insert(V4L2ControlOrDefault<uint8_t>(
246       ControlType::kMenu,
247       ANDROID_CONTROL_SCENE_MODE,
248       ANDROID_CONTROL_AVAILABLE_SCENE_MODES,
249       device,
250       V4L2_CID_SCENE_MODE,
251       std::shared_ptr<ConverterInterface<uint8_t, int32_t>>(new EnumConverter(
252           {{V4L2_SCENE_MODE_NONE, ANDROID_CONTROL_SCENE_MODE_DISABLED},
253            {V4L2_SCENE_MODE_BEACH_SNOW, ANDROID_CONTROL_SCENE_MODE_BEACH},
254            {V4L2_SCENE_MODE_BEACH_SNOW, ANDROID_CONTROL_SCENE_MODE_SNOW},
255            {V4L2_SCENE_MODE_CANDLE_LIGHT,
256             ANDROID_CONTROL_SCENE_MODE_CANDLELIGHT},
257            {V4L2_SCENE_MODE_FIREWORKS, ANDROID_CONTROL_SCENE_MODE_FIREWORKS},
258            {V4L2_SCENE_MODE_LANDSCAPE, ANDROID_CONTROL_SCENE_MODE_LANDSCAPE},
259            {V4L2_SCENE_MODE_NIGHT, ANDROID_CONTROL_SCENE_MODE_NIGHT},
260            {V4L2_SCENE_MODE_PARTY_INDOOR, ANDROID_CONTROL_SCENE_MODE_PARTY},
261            {V4L2_SCENE_MODE_SPORTS, ANDROID_CONTROL_SCENE_MODE_SPORTS},
262            {V4L2_SCENE_MODE_SUNSET, ANDROID_CONTROL_SCENE_MODE_SUNSET}})),
263       ANDROID_CONTROL_SCENE_MODE_DISABLED));
264   // TODO(b/31022612): Scene mode overrides.
265   // Modes from each API that don't match up:
266   // Android: POSTERIZE, WHITEBOARD, BLACKBOARD.
267   // V4L2: ANTIQUE, ART_FREEZE, EMBOSS, GRASS_GREEN, SKETCH, SKIN_WHITEN,
268   // SKY_BLUE, SILHOUETTE, VIVID, SET_CBCR.
269   components.insert(V4L2ControlOrDefault<uint8_t>(
270       ControlType::kMenu,
271       ANDROID_CONTROL_EFFECT_MODE,
272       ANDROID_CONTROL_AVAILABLE_EFFECTS,
273       device,
274       V4L2_CID_COLORFX,
275       std::shared_ptr<ConverterInterface<uint8_t, int32_t>>(new EnumConverter(
276           {{V4L2_COLORFX_NONE, ANDROID_CONTROL_EFFECT_MODE_OFF},
277            {V4L2_COLORFX_BW, ANDROID_CONTROL_EFFECT_MODE_MONO},
278            {V4L2_COLORFX_NEGATIVE, ANDROID_CONTROL_EFFECT_MODE_NEGATIVE},
279            {V4L2_COLORFX_SOLARIZATION, ANDROID_CONTROL_EFFECT_MODE_SOLARIZE},
280            {V4L2_COLORFX_SEPIA, ANDROID_CONTROL_EFFECT_MODE_SEPIA},
281            {V4L2_COLORFX_AQUA, ANDROID_CONTROL_EFFECT_MODE_AQUA}})),
282       ANDROID_CONTROL_EFFECT_MODE_OFF));
283   // TODO(b/31021654): This should behave as a top level switch, not no effect.
284   // Should enforce being set to USE_SCENE_MODE when a scene mode is requested.
285   components.insert(NoEffectMenuControl<uint8_t>(
286       ANDROID_CONTROL_MODE,
287       ANDROID_CONTROL_AVAILABLE_MODES,
288       {ANDROID_CONTROL_MODE_AUTO, ANDROID_CONTROL_MODE_USE_SCENE_MODE}));
289 
290   // Not sure if V4L2 does or doesn't do this, but HAL documentation says
291   // all devices must support FAST, and FAST can be equivalent to OFF, so
292   // either way it's fine to list. And if FAST is included, HIGH_QUALITY
293   // is supposed to be included as well.
294   components.insert(NoEffectMenuControl<uint8_t>(
295       ANDROID_EDGE_MODE,
296       ANDROID_EDGE_AVAILABLE_EDGE_MODES,
297       {ANDROID_EDGE_MODE_FAST, ANDROID_EDGE_MODE_HIGH_QUALITY},
298       {{CAMERA3_TEMPLATE_STILL_CAPTURE, ANDROID_EDGE_MODE_HIGH_QUALITY},
299        {OTHER_TEMPLATES, ANDROID_EDGE_MODE_FAST}}));
300 
301   // TODO(b/31023454): subcomponents of flash.
302   components.insert(
303       std::unique_ptr<PartialMetadataInterface>(new Property<uint8_t>(
304           ANDROID_FLASH_INFO_AVAILABLE, ANDROID_FLASH_INFO_AVAILABLE_FALSE)));
305   components.insert(FixedState<uint8_t>(ANDROID_FLASH_STATE,
306                                         ANDROID_FLASH_STATE_UNAVAILABLE));
307   components.insert(NoEffectMenuControl<uint8_t>(
308       ANDROID_FLASH_MODE, DO_NOT_REPORT_OPTIONS, {ANDROID_FLASH_MODE_OFF}));
309 
310   // TODO(30510395): subcomponents of hotpixel.
311   // No known V4L2 hot pixel correction. But it might be happening,
312   // so we report FAST/HIGH_QUALITY.
313   components.insert(NoEffectMenuControl<uint8_t>(
314       ANDROID_HOT_PIXEL_MODE,
315       ANDROID_HOT_PIXEL_AVAILABLE_HOT_PIXEL_MODES,
316       {ANDROID_HOT_PIXEL_MODE_FAST, ANDROID_HOT_PIXEL_MODE_HIGH_QUALITY}));
317   // ON only needs to be supported for RAW capable devices.
318   components.insert(NoEffectMenuControl<uint8_t>(
319       ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE,
320       ANDROID_STATISTICS_INFO_AVAILABLE_HOT_PIXEL_MAP_MODES,
321       {ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF}));
322 
323   // TODO(30510395): subcomponents focus/lens.
324   // No way to actually get the aperture and focal length
325   // in V4L2, but they're required keys, so fake them.
326   components.insert(
327       NoEffectMenuControl<float>(ANDROID_LENS_APERTURE,
328                                  ANDROID_LENS_INFO_AVAILABLE_APERTURES,
329                                  {2.0}));  // RPi camera v2 is f/2.0.
330   // Always assume external-facing.
331   components.insert(
332       std::unique_ptr<PartialMetadataInterface>(new Property<uint8_t>(
333           ANDROID_LENS_FACING, ANDROID_LENS_FACING_EXTERNAL)));
334   components.insert(
335       NoEffectMenuControl<float>(ANDROID_LENS_FOCAL_LENGTH,
336                                  ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS,
337                                  {3.04}));  // RPi camera v2 is 3.04mm.
338   // No known way to get filter densities from V4L2,
339   // report 0 to indicate this control is not supported.
340   components.insert(
341       NoEffectMenuControl<float>(ANDROID_LENS_FILTER_DENSITY,
342                                  ANDROID_LENS_INFO_AVAILABLE_FILTER_DENSITIES,
343                                  {0.0}));
344   // V4L2 focal units do not correspond to a particular physical unit.
345   components.insert(
346       std::unique_ptr<PartialMetadataInterface>(new Property<uint8_t>(
347           ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION,
348           ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION_UNCALIBRATED)));
349   // TODO(b/31022711): Focus distance component.
350   // Using a NoEffectMenuControl for now because for
351   // fixed-focus it meets expectations. Framework may allow
352   // setting any value and expect it to be clamped to 0, in which
353   // case this will have unexpected behavior (failing on non-0 settings).
354   components.insert(
355       NoEffectMenuControl<float>(ANDROID_LENS_FOCUS_DISTANCE,
356                                  ANDROID_LENS_INFO_MINIMUM_FOCUS_DISTANCE,
357                                  {0}));
358   // Hypefocal distance doesn't mean much for a fixed-focus uncalibrated device.
359   components.insert(std::make_unique<Property<float>>(
360       ANDROID_LENS_INFO_HYPERFOCAL_DISTANCE, 0));
361 
362   // No way to know when the lens is moving or not in V4L2.
363   components.insert(
364       FixedState<uint8_t>(ANDROID_LENS_STATE, ANDROID_LENS_STATE_STATIONARY));
365   // No known V4L2 lens shading. But it might be happening,
366   // so report FAST/HIGH_QUALITY.
367   components.insert(NoEffectMenuControl<uint8_t>(
368       ANDROID_SHADING_MODE,
369       ANDROID_SHADING_AVAILABLE_MODES,
370       {ANDROID_SHADING_MODE_FAST, ANDROID_SHADING_MODE_HIGH_QUALITY}));
371   // ON only needs to be supported for RAW capable devices.
372   components.insert(NoEffectMenuControl<uint8_t>(
373       ANDROID_STATISTICS_LENS_SHADING_MAP_MODE,
374       ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES,
375       {ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF}));
376   // V4L2 doesn't differentiate between OPTICAL and VIDEO stabilization,
377   // so only report one (and report the other as OFF).
378   components.insert(V4L2ControlOrDefault<uint8_t>(
379       ControlType::kMenu,
380       ANDROID_CONTROL_VIDEO_STABILIZATION_MODE,
381       ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES,
382       device,
383       V4L2_CID_IMAGE_STABILIZATION,
384       std::shared_ptr<ConverterInterface<uint8_t, int32_t>>(new EnumConverter(
385           {{0, ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF},
386            {1, ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_ON}})),
387       ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF));
388   components.insert(NoEffectMenuControl<uint8_t>(
389       ANDROID_LENS_OPTICAL_STABILIZATION_MODE,
390       ANDROID_LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION,
391       {ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF}));
392   // TODO(b/31017806): This should definitely have a different default depending
393   // on template.
394   components.insert(NoEffectMenuControl<uint8_t>(
395       ANDROID_CONTROL_CAPTURE_INTENT,
396       DO_NOT_REPORT_OPTIONS,
397       {ANDROID_CONTROL_CAPTURE_INTENT_CUSTOM,
398        ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW,
399        ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE,
400        ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD,
401        ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT,
402        ANDROID_CONTROL_CAPTURE_INTENT_ZERO_SHUTTER_LAG,
403        ANDROID_CONTROL_CAPTURE_INTENT_MANUAL},
404       {{CAMERA3_TEMPLATE_PREVIEW, ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW},
405        {CAMERA3_TEMPLATE_STILL_CAPTURE,
406         ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE},
407        {CAMERA3_TEMPLATE_VIDEO_RECORD,
408         ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD},
409        {CAMERA3_TEMPLATE_VIDEO_SNAPSHOT,
410         ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT},
411        {CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG,
412         ANDROID_CONTROL_CAPTURE_INTENT_ZERO_SHUTTER_LAG},
413        {CAMERA3_TEMPLATE_MANUAL, ANDROID_CONTROL_CAPTURE_INTENT_MANUAL},
414        {OTHER_TEMPLATES, ANDROID_CONTROL_CAPTURE_INTENT_CUSTOM}}));
415 
416   // Unable to control noise reduction in V4L2 devices,
417   // but FAST is allowed to be the same as OFF,
418   // and HIGH_QUALITY can be the same as FAST.
419   components.insert(NoEffectMenuControl<uint8_t>(
420       ANDROID_NOISE_REDUCTION_MODE,
421       ANDROID_NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES,
422       {ANDROID_NOISE_REDUCTION_MODE_FAST,
423        ANDROID_NOISE_REDUCTION_MODE_HIGH_QUALITY},
424       {{CAMERA3_TEMPLATE_STILL_CAPTURE,
425         ANDROID_NOISE_REDUCTION_MODE_HIGH_QUALITY},
426        {OTHER_TEMPLATES, ANDROID_NOISE_REDUCTION_MODE_FAST}}));
427 
428   // TODO(30510395): subcomponents of formats/streams.
429   // For now, no thumbnails available (only [0,0], the "no thumbnail" size).
430   // TODO(b/29580107): Could end up with a mismatch between request & result,
431   // since V4L2 doesn't actually allow for thumbnail size control.
432   components.insert(NoEffectMenuControl<std::array<int32_t, 2>>(
433       ANDROID_JPEG_THUMBNAIL_SIZE,
434       ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES,
435       {{{0, 0}}}));
436   // TODO(b/31022752): Get this from the device,
437   // not constant (from V4L2Gralloc.h).
438   components.insert(std::unique_ptr<PartialMetadataInterface>(
439       new Property<int32_t>(ANDROID_JPEG_MAX_SIZE, V4L2_MAX_JPEG_SIZE)));
440   // TODO(b/31021672): Other JPEG controls (GPS, quality, orientation).
441   // TODO(b/29939583): V4L2 can only support 1 stream at a time.
442   // For now, just reporting minimum allowable for LIMITED devices.
443   components.insert(std::unique_ptr<PartialMetadataInterface>(
444       new Property<std::array<int32_t, 3>>(
445           ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS,
446           {{/* Raw */ 0, /* Non-stalling */ 2, /* Stalling */ 1}})));
447   // Reprocessing not supported.
448   components.insert(std::unique_ptr<PartialMetadataInterface>(
449       new Property<int32_t>(ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS, 0)));
450   // No way to know pipeline depth for V4L2, so fake with max allowable latency.
451   // Doesn't mean much without per-frame controls anyways.
452   components.insert(std::unique_ptr<PartialMetadataInterface>(
453       new Property<uint8_t>(ANDROID_REQUEST_PIPELINE_MAX_DEPTH, 4)));
454   components.insert(FixedState<uint8_t>(ANDROID_REQUEST_PIPELINE_DEPTH, 4));
455   // "LIMITED devices are strongly encouraged to use a non-negative value.
456   // If UNKNOWN is used here then app developers do not have a way to know
457   // when sensor settings have been applied." - Unfortunately, V4L2 doesn't
458   // really help here either. Could even be that adjusting settings mid-stream
459   // blocks in V4L2, and should be avoided.
460   components.insert(
461       std::unique_ptr<PartialMetadataInterface>(new Property<int32_t>(
462           ANDROID_SYNC_MAX_LATENCY, ANDROID_SYNC_MAX_LATENCY_UNKNOWN)));
463   // Never know when controls are synced.
464   components.insert(FixedState<int64_t>(ANDROID_SYNC_FRAME_NUMBER,
465                                         ANDROID_SYNC_FRAME_NUMBER_UNKNOWN));
466 
467   // TODO(b/31022480): subcomponents of cropping/sensors.
468   // Need ANDROID_SCALER_CROP_REGION control support.
469   // V4L2 VIDIOC_CROPCAP doesn't give a way to query this;
470   // it's driver dependent. For now, assume freeform, and
471   // some cameras may just behave badly.
472   // TODO(b/29579652): Figure out a way to determine this.
473   components.insert(std::unique_ptr<PartialMetadataInterface>(
474       new Property<float>(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM, 1)));
475   components.insert(std::unique_ptr<PartialMetadataInterface>(
476       new Property<uint8_t>(ANDROID_SCALER_CROPPING_TYPE,
477                             ANDROID_SCALER_CROPPING_TYPE_FREEFORM)));
478   // Spoof pixel array size for now, eventually get from CROPCAP.
479   std::array<int32_t, 2> pixel_array_size = {{640, 480}};
480   components.insert(std::unique_ptr<PartialMetadataInterface>(
481       new Property<std::array<int32_t, 2>>(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE,
482                                            pixel_array_size)));
483   // Active array size is {x-offset, y-offset, width, height}, relative to
484   // the pixel array size, with {0, 0} being the top left. Since there's no way
485   // to get this in V4L2, assume the full pixel array is the active array.
486   std::array<int32_t, 4> active_array_size = {
487       {0, 0, pixel_array_size[0], pixel_array_size[1]}};
488   components.insert(std::unique_ptr<PartialMetadataInterface>(
489       new Property<std::array<int32_t, 4>>(
490           ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE, active_array_size)));
491   // This is really more freeform than a menu control, but since we're
492   // restricting it to not being used anyways this works for now.
493   components.insert(NoEffectMenuControl<std::array<int32_t, 4>>(
494       ANDROID_SCALER_CROP_REGION, DO_NOT_REPORT_OPTIONS, {active_array_size}));
495   // No way to get in V4L2, so faked. RPi camera v2 is 3.674 x 2.760 mm.
496   // Physical size is used in framework calculations (field of view,
497   // pixel pitch, etc.), so faking it may have unexpected results.
498   components.insert(std::unique_ptr<PartialMetadataInterface>(
499       new Property<std::array<float, 2>>(ANDROID_SENSOR_INFO_PHYSICAL_SIZE,
500                                          {{3.674, 2.760}})));
501   // HAL uses BOOTTIME timestamps.
502   // TODO(b/29457051): make sure timestamps are consistent throughout the HAL.
503   components.insert(std::unique_ptr<PartialMetadataInterface>(
504       new Property<uint8_t>(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE,
505                             ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN)));
506   components.insert(std::make_unique<State<int64_t>>(
507       ANDROID_SENSOR_TIMESTAMP, std::make_unique<BoottimeStateDelegate>()));
508   // No way to actually get shutter skew from V4L2.
509   components.insert(
510       FixedState<int64_t>(ANDROID_SENSOR_ROLLING_SHUTTER_SKEW, 0));
511   // No way to actually get orientation from V4L2.
512   components.insert(std::unique_ptr<PartialMetadataInterface>(
513       new Property<int32_t>(ANDROID_SENSOR_ORIENTATION, 0)));
514   // TODO(b/31023611): Sensor frame duration. Range should
515   // be dependent on the stream configuration being used.
516   // No test patterns supported.
517   components.insert(
518       NoEffectMenuControl<int32_t>(ANDROID_SENSOR_TEST_PATTERN_MODE,
519                                    ANDROID_SENSOR_AVAILABLE_TEST_PATTERN_MODES,
520                                    {ANDROID_SENSOR_TEST_PATTERN_MODE_OFF}));
521 
522   // TODO(b/30510395): subcomponents of face detection.
523   // Face detection not supported.
524   components.insert(NoEffectMenuControl<uint8_t>(
525       ANDROID_STATISTICS_FACE_DETECT_MODE,
526       ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES,
527       {ANDROID_STATISTICS_FACE_DETECT_MODE_OFF}));
528   components.insert(std::unique_ptr<PartialMetadataInterface>(
529       new Property<int32_t>(ANDROID_STATISTICS_INFO_MAX_FACE_COUNT, 0)));
530 
531   // No way to get detected scene flicker from V4L2.
532   components.insert(FixedState<uint8_t>(ANDROID_STATISTICS_SCENE_FLICKER,
533                                         ANDROID_STATISTICS_SCENE_FLICKER_NONE));
534 
535   // TOOD(b/31023265): V4L2_CID_FLASH_INDICATOR_INTENSITY could be queried
536   // to see if there's a transmit LED. Would need to translate HAL off/on
537   // enum to slider min/max value. For now, no LEDs available.
538   components.insert(std::unique_ptr<PartialMetadataInterface>(
539       new Property<uint8_t>(ANDROID_LED_AVAILABLE_LEDS, {})));
540 
541   /* Capabilities. */
542   // The V4L2Metadata pretends to at least meet the
543   // "LIMITED" and "BACKWARD_COMPATIBLE" functionality requirements.
544   components.insert(std::unique_ptr<PartialMetadataInterface>(
545       new Property<uint8_t>(ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL,
546                             ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED)));
547   components.insert(std::unique_ptr<PartialMetadataInterface>(
548       new Property<std::vector<uint8_t>>(
549           ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
550           {ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE})));
551 
552   // Request is unused, and can be any value,
553   // but that value needs to be propagated.
554   components.insert(NoEffectOptionlessControl<int32_t>(ANDROID_REQUEST_ID, 0));
555 
556   // Metadata is returned in a single result; not multiple pieces.
557   components.insert(std::make_unique<Property<int32_t>>(
558       ANDROID_REQUEST_PARTIAL_RESULT_COUNT, 1));
559 
560   int res =
561       AddFormatComponents(device, std::inserter(components, components.end()));
562   if (res) {
563     HAL_LOGE("Failed to initialize format components.");
564     return res;
565   }
566 
567   *result = std::make_unique<Metadata>(std::move(components));
568   return 0;
569 }
570 
571 }  // namespace v4l2_camera_hal
572