1 /* 2 * Copyright (C) 2012 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 #ifndef ANDROID_SERVERS_CAMERA_CAMERA2PARAMETERS_H 18 #define ANDROID_SERVERS_CAMERA_CAMERA2PARAMETERS_H 19 20 #include <system/graphics.h> 21 22 #include <utils/Compat.h> 23 #include <utils/Errors.h> 24 #include <utils/KeyedVector.h> 25 #include <utils/Mutex.h> 26 #include <utils/String8.h> 27 #include <utils/Vector.h> 28 29 #include <camera/CameraParameters.h> 30 #include <camera/CameraParameters2.h> 31 #include <camera/CameraMetadata.h> 32 33 namespace android { 34 namespace camera2 { 35 36 /** 37 * Current camera state; this is the full state of the Camera under the old 38 * camera API (contents of the CameraParameters2 object in a more-efficient 39 * format, plus other state). The enum values are mostly based off the 40 * corresponding camera2 enums, not the camera1 strings. A few are defined here 41 * if they don't cleanly map to camera2 values. 42 */ 43 struct Parameters { 44 /** 45 * Parameters and other state 46 */ 47 int cameraId; 48 int cameraFacing; 49 50 int previewWidth, previewHeight; 51 int32_t previewFpsRange[2]; 52 int previewFormat; 53 54 int previewTransform; // set by CAMERA_CMD_SET_DISPLAY_ORIENTATION 55 56 int pictureWidth, pictureHeight; 57 // Store the picture size before they are overriden by video snapshot 58 int pictureWidthLastSet, pictureHeightLastSet; 59 bool pictureSizeOverriden; 60 61 int32_t jpegThumbSize[2]; 62 uint8_t jpegQuality, jpegThumbQuality; 63 int32_t jpegRotation; 64 65 bool gpsEnabled; 66 double gpsCoordinates[3]; 67 int64_t gpsTimestamp; 68 String8 gpsProcessingMethod; 69 70 uint8_t wbMode; 71 uint8_t effectMode; 72 uint8_t antibandingMode; 73 uint8_t sceneMode; 74 75 enum flashMode_t { 76 FLASH_MODE_OFF = 0, 77 FLASH_MODE_AUTO, 78 FLASH_MODE_ON, 79 FLASH_MODE_TORCH, 80 FLASH_MODE_RED_EYE = ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE, 81 FLASH_MODE_INVALID = -1 82 } flashMode; 83 84 enum focusMode_t { 85 FOCUS_MODE_AUTO = ANDROID_CONTROL_AF_MODE_AUTO, 86 FOCUS_MODE_MACRO = ANDROID_CONTROL_AF_MODE_MACRO, 87 FOCUS_MODE_CONTINUOUS_VIDEO = ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO, 88 FOCUS_MODE_CONTINUOUS_PICTURE = ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE, 89 FOCUS_MODE_EDOF = ANDROID_CONTROL_AF_MODE_EDOF, 90 FOCUS_MODE_INFINITY, 91 FOCUS_MODE_FIXED, 92 FOCUS_MODE_INVALID = -1 93 } focusMode; 94 95 uint8_t focusState; // Latest focus state from HAL 96 97 // For use with triggerAfWithAuto quirk 98 focusMode_t shadowFocusMode; 99 100 struct Area { 101 int left, top, right, bottom; 102 int weight; AreaParameters::Area103 Area() {} AreaParameters::Area104 Area(int left, int top, int right, int bottom, int weight): 105 left(left), top(top), right(right), bottom(bottom), 106 weight(weight) {} isEmptyParameters::Area107 bool isEmpty() const { 108 return (left == 0) && (top == 0) && (right == 0) && (bottom == 0); 109 } 110 }; 111 Vector<Area> focusingAreas; 112 113 struct Size { 114 int32_t width; 115 int32_t height; 116 }; 117 118 struct FpsRange { 119 int32_t low; 120 int32_t high; 121 }; 122 123 int32_t exposureCompensation; 124 bool autoExposureLock; 125 bool autoExposureLockAvailable; 126 bool autoWhiteBalanceLock; 127 bool autoWhiteBalanceLockAvailable; 128 129 // 3A region types, for use with ANDROID_CONTROL_MAX_REGIONS 130 enum region_t { 131 REGION_AE = 0, 132 REGION_AWB, 133 REGION_AF, 134 NUM_REGION // Number of region types 135 } region; 136 137 Vector<Area> meteringAreas; 138 139 int zoom; 140 bool zoomAvailable; 141 142 int videoWidth, videoHeight, videoFormat; 143 android_dataspace videoDataSpace; 144 145 bool recordingHint; 146 bool videoStabilization; 147 148 CameraParameters2 params; 149 String8 paramsFlattened; 150 151 // These parameters are also part of the camera API-visible state, but not 152 // directly listed in Camera.Parameters 153 // One of ICamera::VIDEO_BUFFER_MODE_* 154 int32_t videoBufferMode; 155 bool playShutterSound; 156 bool enableFaceDetect; 157 158 bool enableFocusMoveMessages; 159 int afTriggerCounter; 160 int afStateCounter; 161 int currentAfTriggerId; 162 bool afInMotion; 163 164 int precaptureTriggerCounter; 165 166 int takePictureCounter; 167 168 uint32_t previewCallbackFlags; 169 bool previewCallbackOneShot; 170 bool previewCallbackSurface; 171 172 bool allowZslMode; 173 // Whether the jpeg stream is slower than 30FPS and can slow down preview. 174 // When slowJpegMode is true, allowZslMode must be false to avoid slowing down preview. 175 bool slowJpegMode; 176 // Whether ZSL reprocess is supported by the device. 177 bool isZslReprocessPresent; 178 // Whether the device supports enableZsl. 179 bool isDeviceZslSupported; 180 // Whether the device supports geometric distortion correction 181 bool isDistortionCorrectionSupported; 182 183 // Overall camera state 184 enum State { 185 DISCONNECTED, 186 STOPPED, 187 WAITING_FOR_PREVIEW_WINDOW, 188 PREVIEW, 189 RECORD, 190 STILL_CAPTURE, 191 VIDEO_SNAPSHOT 192 } state; 193 194 // Number of zoom steps to simulate 195 static const unsigned int NUM_ZOOM_STEPS = 100; 196 // Max preview size allowed 197 // This is set to a 1:1 value to allow for any aspect ratio that has 198 // a max long side of 1920 pixels 199 static const unsigned int MAX_PREVIEW_WIDTH = 1920; 200 static const unsigned int MAX_PREVIEW_HEIGHT = 1920; 201 // Initial max preview/recording size bound 202 static const int MAX_INITIAL_PREVIEW_WIDTH = 1920; 203 static const int MAX_INITIAL_PREVIEW_HEIGHT = 1080; 204 // Aspect ratio tolerance 205 static const CONSTEXPR float ASPECT_RATIO_TOLERANCE = 0.001; 206 // Threshold for slow jpeg mode 207 static const int64_t kSlowJpegModeThreshold = 33400000LL; // 33.4 ms 208 // Margin for checking FPS 209 static const int32_t FPS_MARGIN = 1; 210 // Max FPS for default parameters 211 static const int32_t MAX_DEFAULT_FPS = 30; 212 // Minimum FPS for a size to be listed in supported preview/video sizes 213 // Set to slightly less than 30.0 to have some tolerance margin 214 static constexpr double MIN_PREVIEW_RECORD_FPS = 29.97; 215 // Maximum frame duration for a size to be listed in supported preview/video sizes 216 static constexpr int64_t MAX_PREVIEW_RECORD_DURATION_NS = 1e9 / MIN_PREVIEW_RECORD_FPS; 217 218 // Full static camera info, object owned by someone else, such as 219 // Camera2Device. 220 const CameraMetadata *info; 221 222 // Fast-access static device information; this is a subset of the 223 // information available through the staticInfo() method, used for 224 // frequently-accessed values or values that have to be calculated from the 225 // static information. 226 struct DeviceInfo { 227 int32_t arrayWidth; 228 int32_t arrayHeight; 229 int32_t bestStillCaptureFpsRange[2]; 230 uint8_t bestFaceDetectMode; 231 int32_t maxFaces; 232 struct OverrideModes { 233 flashMode_t flashMode; 234 uint8_t wbMode; 235 focusMode_t focusMode; OverrideModesParameters::DeviceInfo::OverrideModes236 OverrideModes(): 237 flashMode(FLASH_MODE_INVALID), 238 wbMode(ANDROID_CONTROL_AWB_MODE_OFF), 239 focusMode(FOCUS_MODE_INVALID) { 240 } 241 }; 242 DefaultKeyedVector<uint8_t, OverrideModes> sceneModeOverrides; 243 bool isExternalCamera; 244 float minFocalLength; 245 bool useFlexibleYuv; 246 Size maxJpegSize; 247 Size maxZslSize; 248 } fastInfo; 249 250 // Quirks information; these are short-lived flags to enable workarounds for 251 // incomplete HAL implementations 252 struct Quirks { 253 bool triggerAfWithAuto; 254 bool useZslFormat; 255 bool meteringCropRegion; 256 bool partialResults; 257 } quirks; 258 259 /** 260 * Parameter manipulation and setup methods 261 */ 262 263 Parameters(int cameraId, int cameraFacing); 264 ~Parameters(); 265 266 // Sets up default parameters 267 status_t initialize(const CameraMetadata *info, int deviceVersion); 268 269 // Build fast-access device static info from static info 270 status_t buildFastInfo(); 271 // Query for quirks from static info 272 status_t buildQuirks(); 273 274 // Get entry from camera static characteristics information. min/maxCount 275 // are used for error checking the number of values in the entry. 0 for 276 // max/minCount means to do no bounds check in that direction. In case of 277 // error, the entry data pointer is null and the count is 0. 278 camera_metadata_ro_entry_t staticInfo(uint32_t tag, 279 size_t minCount=0, size_t maxCount=0, bool required=true) const; 280 281 // Validate and update camera parameters based on new settings 282 status_t set(const String8 ¶mString); 283 284 // Retrieve the current settings 285 String8 get() const; 286 287 // Update passed-in request for common parameters 288 status_t updateRequest(CameraMetadata *request) const; 289 290 // Add/update JPEG entries in metadata 291 status_t updateRequestJpeg(CameraMetadata *request) const; 292 293 /* Helper functions to override jpeg size for video snapshot */ 294 // Override jpeg size by video size. Called during startRecording. 295 status_t overrideJpegSizeByVideoSize(); 296 // Recover overridden jpeg size. Called during stopRecording. 297 status_t recoverOverriddenJpegSize(); 298 // if video snapshot size is currently overridden 299 bool isJpegSizeOverridden(); 300 // whether zero shutter lag should be used for non-recording operation 301 bool useZeroShutterLag() const; 302 303 // Calculate the crop region rectangle, either tightly about the preview 304 // resolution, or a region just based on the active array; both take 305 // into account the current zoom level. 306 struct CropRegion { 307 float left; 308 float top; 309 float width; 310 float height; 311 }; 312 CropRegion calculateCropRegion(bool previewOnly) const; 313 314 // Calculate the field of view of the high-resolution JPEG capture 315 status_t calculatePictureFovs(float *horizFov, float *vertFov) const; 316 317 // Static methods for debugging and converting between camera1 and camera2 318 // parameters 319 320 static const char *getStateName(State state); 321 322 static int formatStringToEnum(const char *format); 323 static const char *formatEnumToString(int format); 324 325 static int wbModeStringToEnum(const char *wbMode); 326 static const char* wbModeEnumToString(uint8_t wbMode); 327 static int effectModeStringToEnum(const char *effectMode); 328 static int abModeStringToEnum(const char *abMode); 329 static int sceneModeStringToEnum(const char *sceneMode); 330 static flashMode_t flashModeStringToEnum(const char *flashMode); 331 static const char* flashModeEnumToString(flashMode_t flashMode); 332 static focusMode_t focusModeStringToEnum(const char *focusMode); 333 static const char* focusModeEnumToString(focusMode_t focusMode); 334 335 static status_t parseAreas(const char *areasCStr, 336 Vector<Area> *areas); 337 338 enum AreaKind 339 { 340 AREA_KIND_FOCUS, 341 AREA_KIND_METERING 342 }; 343 status_t validateAreas(const Vector<Area> &areas, 344 size_t maxRegions, 345 AreaKind areaKind) const; 346 static bool boolFromString(const char *boolStr); 347 348 // Map from camera orientation + facing to gralloc transform enum 349 static int degToTransform(int degrees, bool mirror); 350 351 // API specifies FPS ranges are done in fixed point integer, with LSB = 0.001. 352 // Note that this doesn't apply to the (deprecated) single FPS value. 353 static const int kFpsToApiScale = 1000; 354 355 // Transform from (-1000,-1000)-(1000,1000) normalized coords from camera 356 // API to HAL3 (0,0)-(activePixelArray.width/height) coordinates 357 int normalizedXToArray(int x) const; 358 int normalizedYToArray(int y) const; 359 360 // Transform from HAL3 (0,0)-(activePixelArray.width/height) coordinates to 361 // (-1000,-1000)-(1000,1000) normalized coordinates given a scaler crop 362 // region. 363 int arrayXToNormalizedWithCrop(int x, const CropRegion &scalerCrop) const; 364 int arrayYToNormalizedWithCrop(int y, const CropRegion &scalerCrop) const; 365 366 struct Range { 367 int min; 368 int max; 369 }; 370 371 int32_t fpsFromRange(int32_t min, int32_t max) const; 372 373 private: 374 375 // Convert from viewfinder crop-region relative array coordinates 376 // to HAL3 sensor array coordinates 377 int cropXToArray(int x) const; 378 int cropYToArray(int y) const; 379 380 // Convert from camera API (-1000,1000)-(1000,1000) normalized coords 381 // to viewfinder crop-region relative array coordinates 382 int normalizedXToCrop(int x) const; 383 int normalizedYToCrop(int y) const; 384 385 // Given a scaler crop region, calculate preview crop region based on 386 // preview aspect ratio. 387 CropRegion calculatePreviewCrop(const CropRegion &scalerCrop) const; 388 389 Vector<Size> availablePreviewSizes; 390 Vector<Size> availableVideoSizes; 391 // Get size list (that are no larger than limit) from static metadata. 392 // This method filtered size with minFrameDuration < MAX_PREVIEW_RECORD_DURATION_NS 393 status_t getFilteredSizes(Size limit, Vector<Size> *sizes); 394 // Get max size (from the size array) that matches the given aspect ratio. 395 Size getMaxSizeForRatio(float ratio, const int32_t* sizeArray, size_t count); 396 397 // Helper function for overriding jpeg size for video snapshot 398 // Check if overridden jpeg size needs to be updated after Parameters::set. 399 // The behavior of this function is tailored to the implementation of Parameters::set. 400 // Do not use this function for other purpose. 401 status_t updateOverriddenJpegSize(); 402 403 struct StreamConfiguration { 404 int32_t format; 405 int32_t width; 406 int32_t height; 407 int32_t isInput; 408 }; 409 410 // Helper function extract available stream configuration 411 // Only valid since device HAL version 3.2 412 // returns an empty Vector if device HAL version does support it 413 Vector<StreamConfiguration> getStreamConfigurations(); 414 415 // Helper function to get minimum frame duration for a jpeg size 416 // return -1 if input jpeg size cannot be found in supported size list 417 int64_t getJpegStreamMinFrameDurationNs(Parameters::Size size); 418 419 // Helper function to get minimum frame duration for a size/format combination 420 // return -1 if input size/format combination cannot be found. 421 int64_t getMinFrameDurationNs(Parameters::Size size, int format); 422 423 // Helper function to check if a given fps is supported by all the sizes with 424 // the same format. 425 // return true if the device doesn't support min frame duration metadata tag. 426 bool isFpsSupported(const Vector<Size> &size, int format, int32_t fps); 427 428 // Helper function to get non-duplicated available output formats 429 SortedVector<int32_t> getAvailableOutputFormats(); 430 // Helper function to get available output jpeg sizes 431 Vector<Size> getAvailableJpegSizes(); 432 // Helper function to get maximum size in input Size vector. 433 // The maximum size is defined by comparing width first, when width ties comparing height. 434 Size getMaxSize(const Vector<Size>& sizes); 435 436 int mDeviceVersion; 437 }; 438 439 // This class encapsulates the Parameters class so that it can only be accessed 440 // by constructing a Lock object, which locks the SharedParameter's mutex. 441 class SharedParameters { 442 public: SharedParameters(int cameraId,int cameraFacing)443 SharedParameters(int cameraId, int cameraFacing): 444 mParameters(cameraId, cameraFacing) { 445 } 446 447 template<typename S, typename P> 448 class BaseLock { 449 public: BaseLock(S & p)450 explicit BaseLock(S &p): 451 mParameters(p.mParameters), 452 mSharedParameters(p) { 453 mSharedParameters.mLock.lock(); 454 } 455 ~BaseLock()456 ~BaseLock() { 457 mSharedParameters.mLock.unlock(); 458 } 459 P &mParameters; 460 private: 461 // Disallow copying, default construction 462 BaseLock(); 463 BaseLock(const BaseLock &); 464 BaseLock &operator=(const BaseLock &); 465 S &mSharedParameters; 466 }; 467 typedef BaseLock<SharedParameters, Parameters> Lock; 468 typedef BaseLock<const SharedParameters, const Parameters> ReadLock; 469 470 // Access static info, read-only and immutable, so no lock needed 471 camera_metadata_ro_entry_t staticInfo(uint32_t tag, 472 size_t minCount=0, size_t maxCount=0) const { 473 return mParameters.staticInfo(tag, minCount, maxCount); 474 } 475 476 // Only use for dumping or other debugging unsafeAccess()477 const Parameters &unsafeAccess() { 478 return mParameters; 479 } 480 private: 481 Parameters mParameters; 482 mutable Mutex mLock; 483 }; 484 485 486 }; // namespace camera2 487 }; // namespace android 488 489 #endif 490