Parameters.h revision d309fb9c8a2c4564d88fffba19c4e3688e4b862b
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 33namespace android { 34namespace 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 */ 43struct 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; 103 Area() {} 104 Area(int left, int top, int right, int bottom, int weight): 105 left(left), top(top), right(right), bottom(bottom), 106 weight(weight) {} 107 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 int32_t exposureCompensation; 119 bool autoExposureLock; 120 bool autoWhiteBalanceLock; 121 122 // 3A region types, for use with ANDROID_CONTROL_MAX_REGIONS 123 enum region_t { 124 REGION_AE = 0, 125 REGION_AWB, 126 REGION_AF, 127 NUM_REGION // Number of region types 128 } region; 129 130 Vector<Area> meteringAreas; 131 132 int zoom; 133 134 int videoWidth, videoHeight; 135 136 bool recordingHint; 137 bool videoStabilization; 138 139 CameraParameters2 params; 140 String8 paramsFlattened; 141 142 // These parameters are also part of the camera API-visible state, but not 143 // directly listed in Camera.Parameters 144 bool storeMetadataInBuffers; 145 bool playShutterSound; 146 bool enableFaceDetect; 147 148 bool enableFocusMoveMessages; 149 int afTriggerCounter; 150 int afStateCounter; 151 int currentAfTriggerId; 152 bool afInMotion; 153 154 int precaptureTriggerCounter; 155 156 int takePictureCounter; 157 158 uint32_t previewCallbackFlags; 159 bool previewCallbackOneShot; 160 bool previewCallbackSurface; 161 162 bool zslMode; 163 // Whether the jpeg stream is slower than 30FPS and can slow down preview. 164 // When slowJpegMode is true, zslMode must be false to avoid slowing down preview. 165 bool slowJpegMode; 166 167 // Overall camera state 168 enum State { 169 DISCONNECTED, 170 STOPPED, 171 WAITING_FOR_PREVIEW_WINDOW, 172 PREVIEW, 173 RECORD, 174 STILL_CAPTURE, 175 VIDEO_SNAPSHOT 176 } state; 177 178 // Number of zoom steps to simulate 179 static const unsigned int NUM_ZOOM_STEPS = 100; 180 // Max preview size allowed 181 // This is set to a 1:1 value to allow for any aspect ratio that has 182 // a max long side of 1920 pixels 183 static const unsigned int MAX_PREVIEW_WIDTH = 1920; 184 static const unsigned int MAX_PREVIEW_HEIGHT = 1920; 185 // Initial max preview/recording size bound 186 static const int MAX_INITIAL_PREVIEW_WIDTH = 1920; 187 static const int MAX_INITIAL_PREVIEW_HEIGHT = 1080; 188 // Aspect ratio tolerance 189 static const CONSTEXPR float ASPECT_RATIO_TOLERANCE = 0.001; 190 // Threshold for slow jpeg mode 191 static const int64_t kSlowJpegModeThreshold = 33400000LL; // 33.4 ms 192 193 // Full static camera info, object owned by someone else, such as 194 // Camera2Device. 195 const CameraMetadata *info; 196 197 // Fast-access static device information; this is a subset of the 198 // information available through the staticInfo() method, used for 199 // frequently-accessed values or values that have to be calculated from the 200 // static information. 201 struct DeviceInfo { 202 int32_t arrayWidth; 203 int32_t arrayHeight; 204 int32_t bestStillCaptureFpsRange[2]; 205 uint8_t bestFaceDetectMode; 206 int32_t maxFaces; 207 struct OverrideModes { 208 flashMode_t flashMode; 209 uint8_t wbMode; 210 focusMode_t focusMode; 211 OverrideModes(): 212 flashMode(FLASH_MODE_INVALID), 213 wbMode(ANDROID_CONTROL_AWB_MODE_OFF), 214 focusMode(FOCUS_MODE_INVALID) { 215 } 216 }; 217 DefaultKeyedVector<uint8_t, OverrideModes> sceneModeOverrides; 218 float minFocalLength; 219 bool useFlexibleYuv; 220 } fastInfo; 221 222 // Quirks information; these are short-lived flags to enable workarounds for 223 // incomplete HAL implementations 224 struct Quirks { 225 bool triggerAfWithAuto; 226 bool useZslFormat; 227 bool meteringCropRegion; 228 bool partialResults; 229 } quirks; 230 231 /** 232 * Parameter manipulation and setup methods 233 */ 234 235 Parameters(int cameraId, int cameraFacing); 236 ~Parameters(); 237 238 // Sets up default parameters 239 status_t initialize(const CameraMetadata *info, int deviceVersion); 240 241 // Build fast-access device static info from static info 242 status_t buildFastInfo(); 243 // Query for quirks from static info 244 status_t buildQuirks(); 245 246 // Get entry from camera static characteristics information. min/maxCount 247 // are used for error checking the number of values in the entry. 0 for 248 // max/minCount means to do no bounds check in that direction. In case of 249 // error, the entry data pointer is null and the count is 0. 250 camera_metadata_ro_entry_t staticInfo(uint32_t tag, 251 size_t minCount=0, size_t maxCount=0, bool required=true) const; 252 253 // Validate and update camera parameters based on new settings 254 status_t set(const String8 ¶mString); 255 256 // Retrieve the current settings 257 String8 get() const; 258 259 // Update passed-in request for common parameters 260 status_t updateRequest(CameraMetadata *request) const; 261 262 // Add/update JPEG entries in metadata 263 status_t updateRequestJpeg(CameraMetadata *request) const; 264 265 /* Helper functions to override jpeg size for video snapshot */ 266 // Override jpeg size by video size. Called during startRecording. 267 status_t overrideJpegSizeByVideoSize(); 268 // Recover overridden jpeg size. Called during stopRecording. 269 status_t recoverOverriddenJpegSize(); 270 // if video snapshot size is currently overridden 271 bool isJpegSizeOverridden(); 272 273 // Calculate the crop region rectangle, either tightly about the preview 274 // resolution, or a region just based on the active array; both take 275 // into account the current zoom level. 276 struct CropRegion { 277 float left; 278 float top; 279 float width; 280 float height; 281 }; 282 CropRegion calculateCropRegion(bool previewOnly) const; 283 284 // Calculate the field of view of the high-resolution JPEG capture 285 status_t calculatePictureFovs(float *horizFov, float *vertFov) const; 286 287 // Static methods for debugging and converting between camera1 and camera2 288 // parameters 289 290 static const char *getStateName(State state); 291 292 static int formatStringToEnum(const char *format); 293 static const char *formatEnumToString(int format); 294 295 static int wbModeStringToEnum(const char *wbMode); 296 static const char* wbModeEnumToString(uint8_t wbMode); 297 static int effectModeStringToEnum(const char *effectMode); 298 static int abModeStringToEnum(const char *abMode); 299 static int sceneModeStringToEnum(const char *sceneMode); 300 static flashMode_t flashModeStringToEnum(const char *flashMode); 301 static const char* flashModeEnumToString(flashMode_t flashMode); 302 static focusMode_t focusModeStringToEnum(const char *focusMode); 303 static const char* focusModeEnumToString(focusMode_t focusMode); 304 305 static status_t parseAreas(const char *areasCStr, 306 Vector<Area> *areas); 307 308 enum AreaKind 309 { 310 AREA_KIND_FOCUS, 311 AREA_KIND_METERING 312 }; 313 status_t validateAreas(const Vector<Area> &areas, 314 size_t maxRegions, 315 AreaKind areaKind) const; 316 static bool boolFromString(const char *boolStr); 317 318 // Map from camera orientation + facing to gralloc transform enum 319 static int degToTransform(int degrees, bool mirror); 320 321 // API specifies FPS ranges are done in fixed point integer, with LSB = 0.001. 322 // Note that this doesn't apply to the (deprecated) single FPS value. 323 static const int kFpsToApiScale = 1000; 324 325 // Transform from (-1000,-1000)-(1000,1000) normalized coords from camera 326 // API to HAL3 (0,0)-(activePixelArray.width/height) coordinates 327 int normalizedXToArray(int x) const; 328 int normalizedYToArray(int y) const; 329 330 // Transform from HAL3 (0,0)-(activePixelArray.width/height) coordinates to 331 // (-1000,-1000)-(1000,1000) normalized coordinates given a scaler crop 332 // region. 333 int arrayXToNormalizedWithCrop(int x, const CropRegion &scalerCrop) const; 334 int arrayYToNormalizedWithCrop(int y, const CropRegion &scalerCrop) const; 335 336 struct Range { 337 int min; 338 int max; 339 }; 340 341 int32_t fpsFromRange(int32_t min, int32_t max) const; 342 343private: 344 345 // Convert from viewfinder crop-region relative array coordinates 346 // to HAL3 sensor array coordinates 347 int cropXToArray(int x) const; 348 int cropYToArray(int y) const; 349 350 // Convert from camera API (-1000,1000)-(1000,1000) normalized coords 351 // to viewfinder crop-region relative array coordinates 352 int normalizedXToCrop(int x) const; 353 int normalizedYToCrop(int y) const; 354 355 // Given a scaler crop region, calculate preview crop region based on 356 // preview aspect ratio. 357 CropRegion calculatePreviewCrop(const CropRegion &scalerCrop) const; 358 359 Vector<Size> availablePreviewSizes; 360 Vector<Size> availableVideoSizes; 361 // Get size list (that are no larger than limit) from static metadata. 362 status_t getFilteredSizes(Size limit, Vector<Size> *sizes); 363 // Get max size (from the size array) that matches the given aspect ratio. 364 Size getMaxSizeForRatio(float ratio, const int32_t* sizeArray, size_t count); 365 366 // Helper function for overriding jpeg size for video snapshot 367 // Check if overridden jpeg size needs to be updated after Parameters::set. 368 // The behavior of this function is tailored to the implementation of Parameters::set. 369 // Do not use this function for other purpose. 370 status_t updateOverriddenJpegSize(); 371 372 struct StreamConfiguration { 373 int32_t format; 374 int32_t width; 375 int32_t height; 376 int32_t isInput; 377 }; 378 379 // Helper function extract available stream configuration 380 // Only valid since device HAL version 3.2 381 // returns an empty Vector if device HAL version does support it 382 Vector<StreamConfiguration> getStreamConfigurations(); 383 384 // Helper function to get minimum frame duration for a jpeg size 385 // return -1 if input jpeg size cannot be found in supported size list 386 int64_t getJpegStreamMinFrameDurationNs(Parameters::Size size); 387 388 // Helper function to get non-duplicated available output formats 389 SortedVector<int32_t> getAvailableOutputFormats(); 390 // Helper function to get available output jpeg sizes 391 Vector<Size> getAvailableJpegSizes(); 392 // Helper function to get maximum size in input Size vector. 393 // The maximum size is defined by comparing width first, when width ties comparing height. 394 Size getMaxSize(const Vector<Size>& sizes); 395 396 int mDeviceVersion; 397}; 398 399// This class encapsulates the Parameters class so that it can only be accessed 400// by constructing a Lock object, which locks the SharedParameter's mutex. 401class SharedParameters { 402 public: 403 SharedParameters(int cameraId, int cameraFacing): 404 mParameters(cameraId, cameraFacing) { 405 } 406 407 template<typename S, typename P> 408 class BaseLock { 409 public: 410 BaseLock(S &p): 411 mParameters(p.mParameters), 412 mSharedParameters(p) { 413 mSharedParameters.mLock.lock(); 414 } 415 416 ~BaseLock() { 417 mSharedParameters.mLock.unlock(); 418 } 419 P &mParameters; 420 private: 421 // Disallow copying, default construction 422 BaseLock(); 423 BaseLock(const BaseLock &); 424 BaseLock &operator=(const BaseLock &); 425 S &mSharedParameters; 426 }; 427 typedef BaseLock<SharedParameters, Parameters> Lock; 428 typedef BaseLock<const SharedParameters, const Parameters> ReadLock; 429 430 // Access static info, read-only and immutable, so no lock needed 431 camera_metadata_ro_entry_t staticInfo(uint32_t tag, 432 size_t minCount=0, size_t maxCount=0) const { 433 return mParameters.staticInfo(tag, minCount, maxCount); 434 } 435 436 // Only use for dumping or other debugging 437 const Parameters &unsafeAccess() { 438 return mParameters; 439 } 440 private: 441 Parameters mParameters; 442 mutable Mutex mLock; 443}; 444 445 446}; // namespace camera2 447}; // namespace android 448 449#endif 450