LegacyRequestMapper.java revision 733341bf0db89c93ee1341ddfca9b0c49731c836
1/* 2 * Copyright (C) 2014 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 17package android.hardware.camera2.legacy; 18 19import android.graphics.Rect; 20import android.hardware.Camera; 21import android.hardware.Camera.Parameters; 22import android.hardware.camera2.CameraCharacteristics; 23import android.hardware.camera2.CaptureRequest; 24import android.hardware.camera2.params.MeteringRectangle; 25import android.hardware.camera2.utils.ListUtils; 26import android.hardware.camera2.utils.ParamsUtils; 27import android.util.Log; 28import android.util.Range; 29import android.util.Size; 30 31import java.util.ArrayList; 32import java.util.Arrays; 33import java.util.List; 34import java.util.Objects; 35 36import static com.android.internal.util.Preconditions.*; 37import static android.hardware.camera2.CaptureRequest.*; 38 39/** 40 * Provide legacy-specific implementations of camera2 CaptureRequest for legacy devices. 41 */ 42@SuppressWarnings("deprecation") 43public class LegacyRequestMapper { 44 private static final String TAG = "LegacyRequestMapper"; 45 private static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE); 46 47 /** 48 * Set the legacy parameters using the {@link LegacyRequest legacy request}. 49 * 50 * <p>The legacy request's parameters are changed as a side effect of calling this 51 * method.</p> 52 * 53 * @param legacyRequest a non-{@code null} legacy request 54 */ 55 public static void convertRequestMetadata(LegacyRequest legacyRequest) { 56 CameraCharacteristics characteristics = legacyRequest.characteristics; 57 CaptureRequest request = legacyRequest.captureRequest; 58 Size previewSize = legacyRequest.previewSize; 59 Camera.Parameters params = legacyRequest.parameters; 60 61 Rect activeArray = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE); 62 63 /* 64 * scaler.cropRegion 65 */ 66 ParameterUtils.ZoomData zoomData; 67 { 68 zoomData = ParameterUtils.convertScalerCropRegion(activeArray, 69 request.get(SCALER_CROP_REGION), 70 previewSize, 71 params); 72 73 if (params.isZoomSupported()) { 74 params.setZoom(zoomData.zoomIndex); 75 } else if (VERBOSE) { 76 Log.v(TAG, "convertRequestToMetadata - zoom is not supported"); 77 } 78 } 79 80 81 /* 82 * control.ae* 83 */ 84 // control.aeAntibandingMode 85 { 86 String legacyMode; 87 Integer antiBandingMode = request.get(CONTROL_AE_ANTIBANDING_MODE); 88 if (antiBandingMode != null) { 89 legacyMode = convertAeAntiBandingModeToLegacy(antiBandingMode); 90 } else { 91 legacyMode = ListUtils.listSelectFirstFrom(params.getSupportedAntibanding(), 92 new String[] { 93 Parameters.ANTIBANDING_AUTO, 94 Parameters.ANTIBANDING_OFF, 95 Parameters.ANTIBANDING_50HZ, 96 Parameters.ANTIBANDING_60HZ, 97 }); 98 } 99 100 if (legacyMode != null) { 101 params.setAntibanding(legacyMode); 102 } 103 } 104 105 /* 106 * control.aeRegions, afRegions 107 */ 108 { 109 // aeRegions 110 { 111 // Use aeRegions if available, fall back to using awbRegions if present 112 MeteringRectangle[] aeRegions = request.get(CONTROL_AE_REGIONS); 113 if (request.get(CONTROL_AWB_REGIONS) != null) { 114 Log.w(TAG, "convertRequestMetadata - control.awbRegions setting is not " + 115 "supported, ignoring value"); 116 } 117 int maxNumMeteringAreas = params.getMaxNumMeteringAreas(); 118 List<Camera.Area> meteringAreaList = convertMeteringRegionsToLegacy( 119 activeArray, zoomData, aeRegions, maxNumMeteringAreas, 120 /*regionName*/"AE"); 121 122 params.setMeteringAreas(meteringAreaList); 123 } 124 125 // afRegions 126 { 127 MeteringRectangle[] afRegions = request.get(CONTROL_AF_REGIONS); 128 int maxNumFocusAreas = params.getMaxNumFocusAreas(); 129 List<Camera.Area> focusAreaList = convertMeteringRegionsToLegacy( 130 activeArray, zoomData, afRegions, maxNumFocusAreas, 131 /*regionName*/"AF"); 132 133 params.setFocusAreas(focusAreaList); 134 } 135 } 136 137 // control.aeTargetFpsRange 138 Range<Integer> aeFpsRange = request.get(CONTROL_AE_TARGET_FPS_RANGE); 139 if (aeFpsRange != null) { 140 int[] legacyFps = convertAeFpsRangeToLegacy(aeFpsRange); 141 142 // TODO - Should we enforce that all HAL1 devices must include (30, 30) FPS range? 143 boolean supported = false; 144 for(int[] range : params.getSupportedPreviewFpsRange()) { 145 if (legacyFps[0] == range[0] && legacyFps[1] == range[1]) { 146 supported = true; 147 break; 148 } 149 } 150 if (supported) { 151 params.setPreviewFpsRange(legacyFps[Camera.Parameters.PREVIEW_FPS_MIN_INDEX], 152 legacyFps[Camera.Parameters.PREVIEW_FPS_MAX_INDEX]); 153 params.setRecordingHint(false); 154 } else { 155 Log.w(TAG, "Unsupported FPS range set [" + legacyFps[0] + "," + legacyFps[1] + "]"); 156 params.setRecordingHint(true); 157 } 158 } 159 160 /* 161 * control 162 */ 163 164 // control.aeExposureCompensation 165 { 166 Range<Integer> compensationRange = 167 characteristics.get(CameraCharacteristics.CONTROL_AE_COMPENSATION_RANGE); 168 int compensation = ParamsUtils.getOrDefault(request, 169 CONTROL_AE_EXPOSURE_COMPENSATION, 170 /*defaultValue*/0); 171 172 if (!compensationRange.contains(compensation)) { 173 Log.w(TAG, 174 "convertRequestMetadata - control.aeExposureCompensation " + 175 "is out of range, ignoring value"); 176 compensation = 0; 177 } 178 179 params.setExposureCompensation(compensation); 180 } 181 182 // control.aeLock 183 { 184 Boolean aeLock = getIfSupported(request, CONTROL_AE_LOCK, /*defaultValue*/false, 185 params.isAutoExposureLockSupported(), 186 /*allowedValue*/false); 187 188 if (aeLock != null) { 189 params.setAutoExposureLock(aeLock); 190 } 191 192 if (VERBOSE) { 193 Log.v(TAG, "convertRequestToMetadata - control.aeLock set to " + aeLock); 194 } 195 196 // TODO: Don't add control.aeLock to availableRequestKeys if it's not supported 197 } 198 199 // control.aeMode, flash.mode 200 mapAeAndFlashMode(request, /*out*/params); 201 202 // control.afMode 203 { 204 int afMode = ParamsUtils.getOrDefault(request, CONTROL_AF_MODE, 205 /*defaultValue*/CONTROL_AF_MODE_OFF); 206 String focusMode = LegacyMetadataMapper.convertAfModeToLegacy(afMode, 207 params.getSupportedFocusModes()); 208 209 if (focusMode != null) { 210 params.setFocusMode(focusMode); 211 } 212 213 if (VERBOSE) { 214 Log.v(TAG, "convertRequestToMetadata - control.afMode " 215 + afMode + " mapped to " + focusMode); 216 } 217 } 218 219 // control.awbMode 220 { 221 Integer awbMode = getIfSupported(request, CONTROL_AWB_MODE, 222 /*defaultValue*/CONTROL_AWB_MODE_AUTO, 223 params.getSupportedWhiteBalance() != null, 224 /*allowedValue*/CONTROL_AWB_MODE_AUTO); 225 226 String whiteBalanceMode = null; 227 if (awbMode != null) { // null iff AWB is not supported by camera1 api 228 whiteBalanceMode = convertAwbModeToLegacy(awbMode); 229 params.setWhiteBalance(whiteBalanceMode); 230 } 231 232 if (VERBOSE) { 233 Log.v(TAG, "convertRequestToMetadata - control.awbMode " 234 + awbMode + " mapped to " + whiteBalanceMode); 235 } 236 } 237 238 // control.awbLock 239 { 240 Boolean awbLock = getIfSupported(request, CONTROL_AWB_LOCK, /*defaultValue*/false, 241 params.isAutoWhiteBalanceLockSupported(), 242 /*allowedValue*/false); 243 244 if (awbLock != null) { 245 params.setAutoWhiteBalanceLock(awbLock); 246 } 247 248 // TODO: Don't add control.awbLock to availableRequestKeys if it's not supported 249 } 250 251 // lens.focusDistance 252 { 253 boolean infinityFocusSupported = 254 ListUtils.listContains(params.getSupportedFocusModes(), 255 Parameters.FOCUS_MODE_INFINITY); 256 Float focusDistance = getIfSupported(request, LENS_FOCUS_DISTANCE, 257 /*defaultValue*/0f, infinityFocusSupported, /*allowedValue*/0f); 258 259 if (focusDistance == null || focusDistance != 0f) { 260 Log.w(TAG, 261 "convertRequestToMetadata - Ignoring android.lens.focusDistance " 262 + infinityFocusSupported + ", only 0.0f is supported"); 263 } 264 } 265 266 // control.sceneMode, control.mode 267 { 268 // TODO: Map FACE_PRIORITY scene mode to face detection. 269 270 if (params.getSupportedSceneModes() != null) { 271 int controlMode = ParamsUtils.getOrDefault(request, CONTROL_MODE, 272 /*defaultValue*/CONTROL_MODE_AUTO); 273 String modeToSet; 274 switch (controlMode) { 275 case CONTROL_MODE_USE_SCENE_MODE: { 276 int sceneMode = ParamsUtils.getOrDefault(request, CONTROL_SCENE_MODE, 277 /*defaultValue*/CONTROL_SCENE_MODE_DISABLED); 278 String legacySceneMode = LegacyMetadataMapper. 279 convertSceneModeToLegacy(sceneMode); 280 if (legacySceneMode != null) { 281 modeToSet = legacySceneMode; 282 } else { 283 modeToSet = Parameters.SCENE_MODE_AUTO; 284 Log.w(TAG, "Skipping unknown requested scene mode: " + sceneMode); 285 } 286 break; 287 } 288 case CONTROL_MODE_AUTO: { 289 modeToSet = Parameters.SCENE_MODE_AUTO; 290 break; 291 } 292 default: { 293 Log.w(TAG, "Control mode " + controlMode + 294 " is unsupported, defaulting to AUTO"); 295 modeToSet = Parameters.SCENE_MODE_AUTO; 296 } 297 } 298 params.setSceneMode(modeToSet); 299 } 300 } 301 302 // control.effectMode 303 { 304 if (params.getSupportedColorEffects() != null) { 305 int effectMode = ParamsUtils.getOrDefault(request, CONTROL_EFFECT_MODE, 306 /*defaultValue*/CONTROL_EFFECT_MODE_OFF); 307 String legacyEffectMode = LegacyMetadataMapper.convertEffectModeToLegacy(effectMode); 308 if (legacyEffectMode != null) { 309 params.setColorEffect(legacyEffectMode); 310 } else { 311 params.setColorEffect(Parameters.EFFECT_NONE); 312 Log.w(TAG, "Skipping unknown requested effect mode: " + effectMode); 313 } 314 } 315 } 316 317 /* 318 * sensor 319 */ 320 321 // sensor.testPattern 322 { 323 int testPatternMode = ParamsUtils.getOrDefault(request, SENSOR_TEST_PATTERN_MODE, 324 /*defaultValue*/SENSOR_TEST_PATTERN_MODE_OFF); 325 if (testPatternMode != SENSOR_TEST_PATTERN_MODE_OFF) { 326 Log.w(TAG, "convertRequestToMetadata - ignoring sensor.testPatternMode " 327 + testPatternMode + "; only OFF is supported"); 328 } 329 } 330 } 331 332 private static List<Camera.Area> convertMeteringRegionsToLegacy( 333 Rect activeArray, ParameterUtils.ZoomData zoomData, 334 MeteringRectangle[] meteringRegions, int maxNumMeteringAreas, String regionName) { 335 if (meteringRegions == null || maxNumMeteringAreas <= 0) { 336 if (maxNumMeteringAreas > 0) { 337 return Arrays.asList(ParameterUtils.CAMERA_AREA_DEFAULT); 338 } else { 339 return null; 340 } 341 } 342 343 // Add all non-zero weight regions to the list 344 List<MeteringRectangle> meteringRectangleList = new ArrayList<>(); 345 for (MeteringRectangle rect : meteringRegions) { 346 if (rect.getMeteringWeight() != MeteringRectangle.METERING_WEIGHT_DONT_CARE) { 347 meteringRectangleList.add(rect); 348 } 349 } 350 351 // Ignore any regions beyond our maximum supported count 352 int countMeteringAreas = 353 Math.min(maxNumMeteringAreas, meteringRectangleList.size()); 354 List<Camera.Area> meteringAreaList = new ArrayList<>(countMeteringAreas); 355 356 for (int i = 0; i < countMeteringAreas; ++i) { 357 MeteringRectangle rect = meteringRectangleList.get(i); 358 359 ParameterUtils.MeteringData meteringData = 360 ParameterUtils.convertMeteringRectangleToLegacy(activeArray, rect, zoomData); 361 meteringAreaList.add(meteringData.meteringArea); 362 } 363 364 if (maxNumMeteringAreas < meteringRectangleList.size()) { 365 Log.w(TAG, 366 "convertMeteringRegionsToLegacy - Too many requested " + regionName + 367 " regions, ignoring all beyond the first " + maxNumMeteringAreas); 368 } 369 370 if (VERBOSE) { 371 Log.v(TAG, "convertMeteringRegionsToLegacy - " + regionName + " areas = " 372 + ParameterUtils.stringFromAreaList(meteringAreaList)); 373 } 374 375 return meteringAreaList; 376 } 377 378 private static void mapAeAndFlashMode(CaptureRequest r, /*out*/Parameters p) { 379 int flashMode = ParamsUtils.getOrDefault(r, FLASH_MODE, FLASH_MODE_OFF); 380 int aeMode = ParamsUtils.getOrDefault(r, CONTROL_AE_MODE, CONTROL_AE_MODE_ON); 381 382 List<String> supportedFlashModes = p.getSupportedFlashModes(); 383 384 String flashModeSetting = null; 385 386 // Flash is OFF by default, on cameras that support flash 387 if (ListUtils.listContains(supportedFlashModes, Parameters.FLASH_MODE_OFF)) { 388 flashModeSetting = Parameters.FLASH_MODE_OFF; 389 } 390 391 /* 392 * Map all of the control.aeMode* enums, but ignore AE_MODE_OFF since we never support it 393 */ 394 395 // Ignore flash.mode controls unless aeMode == ON 396 if (aeMode == CONTROL_AE_MODE_ON) { 397 if (flashMode == FLASH_MODE_TORCH) { 398 if (ListUtils.listContains(supportedFlashModes, Parameters.FLASH_MODE_TORCH)) { 399 flashModeSetting = Parameters.FLASH_MODE_TORCH; 400 } else { 401 Log.w(TAG, "mapAeAndFlashMode - Ignore flash.mode == TORCH;" + 402 "camera does not support it"); 403 } 404 } else if (flashMode == FLASH_MODE_SINGLE) { 405 if (ListUtils.listContains(supportedFlashModes, Parameters.FLASH_MODE_ON)) { 406 flashModeSetting = Parameters.FLASH_MODE_ON; 407 } else { 408 Log.w(TAG, "mapAeAndFlashMode - Ignore flash.mode == SINGLE;" + 409 "camera does not support it"); 410 } 411 } else { 412 // Use the default FLASH_MODE_OFF 413 } 414 } else if (aeMode == CONTROL_AE_MODE_ON_ALWAYS_FLASH) { 415 if (ListUtils.listContains(supportedFlashModes, Parameters.FLASH_MODE_ON)) { 416 flashModeSetting = Parameters.FLASH_MODE_ON; 417 } else { 418 Log.w(TAG, "mapAeAndFlashMode - Ignore control.aeMode == ON_ALWAYS_FLASH;" + 419 "camera does not support it"); 420 } 421 } else if (aeMode == CONTROL_AE_MODE_ON_AUTO_FLASH) { 422 if (ListUtils.listContains(supportedFlashModes, Parameters.FLASH_MODE_AUTO)) { 423 flashModeSetting = Parameters.FLASH_MODE_AUTO; 424 } else { 425 Log.w(TAG, "mapAeAndFlashMode - Ignore control.aeMode == ON_AUTO_FLASH;" + 426 "camera does not support it"); 427 } 428 } else if (aeMode == CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE) { 429 if (ListUtils.listContains(supportedFlashModes, Parameters.FLASH_MODE_RED_EYE)) { 430 flashModeSetting = Parameters.FLASH_MODE_RED_EYE; 431 } else { 432 Log.w(TAG, "mapAeAndFlashMode - Ignore control.aeMode == ON_AUTO_FLASH_REDEYE;" 433 + "camera does not support it"); 434 } 435 } else { 436 // Default to aeMode == ON, flash = OFF 437 } 438 439 if (flashModeSetting != null) { 440 p.setFlashMode(flashModeSetting); 441 } 442 443 if (VERBOSE) { 444 Log.v(TAG, 445 "mapAeAndFlashMode - set flash.mode (api1) to " + flashModeSetting 446 + ", requested (api2) " + flashMode 447 + ", supported (api1) " + ListUtils.listToString(supportedFlashModes)); 448 } 449 } 450 451 /** 452 * Returns null if the anti-banding mode enum is not supported. 453 */ 454 private static String convertAeAntiBandingModeToLegacy(int mode) { 455 switch (mode) { 456 case CONTROL_AE_ANTIBANDING_MODE_OFF: { 457 return Parameters.ANTIBANDING_OFF; 458 } 459 case CONTROL_AE_ANTIBANDING_MODE_50HZ: { 460 return Parameters.ANTIBANDING_50HZ; 461 } 462 case CONTROL_AE_ANTIBANDING_MODE_60HZ: { 463 return Parameters.ANTIBANDING_60HZ; 464 } 465 case CONTROL_AE_ANTIBANDING_MODE_AUTO: { 466 return Parameters.ANTIBANDING_AUTO; 467 } 468 default: { 469 return null; 470 } 471 } 472 } 473 474 private static int[] convertAeFpsRangeToLegacy(Range<Integer> fpsRange) { 475 int[] legacyFps = new int[2]; 476 legacyFps[Parameters.PREVIEW_FPS_MIN_INDEX] = fpsRange.getLower(); 477 legacyFps[Parameters.PREVIEW_FPS_MAX_INDEX] = fpsRange.getUpper(); 478 return legacyFps; 479 } 480 481 private static String convertAwbModeToLegacy(int mode) { 482 switch (mode) { 483 case CONTROL_AWB_MODE_AUTO: 484 return Camera.Parameters.WHITE_BALANCE_AUTO; 485 case CONTROL_AWB_MODE_INCANDESCENT: 486 return Camera.Parameters.WHITE_BALANCE_INCANDESCENT; 487 case CONTROL_AWB_MODE_FLUORESCENT: 488 return Camera.Parameters.WHITE_BALANCE_FLUORESCENT; 489 case CONTROL_AWB_MODE_WARM_FLUORESCENT: 490 return Camera.Parameters.WHITE_BALANCE_WARM_FLUORESCENT; 491 case CONTROL_AWB_MODE_DAYLIGHT: 492 return Camera.Parameters.WHITE_BALANCE_DAYLIGHT; 493 case CONTROL_AWB_MODE_CLOUDY_DAYLIGHT: 494 return Camera.Parameters.WHITE_BALANCE_CLOUDY_DAYLIGHT; 495 case CONTROL_AWB_MODE_TWILIGHT: 496 return Camera.Parameters.WHITE_BALANCE_TWILIGHT; 497 default: 498 Log.w(TAG, "convertAwbModeToLegacy - unrecognized control.awbMode" + mode); 499 return Camera.Parameters.WHITE_BALANCE_AUTO; 500 } 501 } 502 503 504 /** 505 * Return {@code null} if the value is not supported, otherwise return the retrieved key's 506 * value from the request (or the default value if it wasn't set). 507 * 508 * <p>If the fetched value in the request is equivalent to {@code allowedValue}, 509 * then omit the warning (e.g. turning off AF lock on a camera 510 * that always has the AF lock turned off is a silent no-op), but still return {@code null}.</p> 511 * 512 * <p>Logs a warning to logcat if the key is not supported by api1 camera device.</p. 513 */ 514 private static <T> T getIfSupported( 515 CaptureRequest r, CaptureRequest.Key<T> key, T defaultValue, boolean isSupported, 516 T allowedValue) { 517 T val = ParamsUtils.getOrDefault(r, key, defaultValue); 518 519 if (!isSupported) { 520 if (!Objects.equals(val, allowedValue)) { 521 Log.w(TAG, key.getName() + " is not supported; ignoring requested value " + val); 522 } 523 return null; 524 } 525 526 return val; 527 } 528} 529