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