Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 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> |
Eino-Ville Talvala | 8a42dd8 | 2012-10-02 13:30:04 -0700 | [diff] [blame] | 26 | #include <utils/KeyedVector.h> |
| 27 | #include <camera/CameraParameters.h> |
Igor Murashkin | 7efa520 | 2013-02-13 15:53:56 -0800 | [diff] [blame^] | 28 | #include <camera/CameraMetadata.h> |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | namespace camera2 { |
| 32 | |
Eino-Ville Talvala | da6665c | 2012-08-29 17:37:16 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Current camera state; this is the full state of the Camera under the old |
| 35 | * camera API (contents of the CameraParameters object in a more-efficient |
| 36 | * format, plus other state). The enum values are mostly based off the |
| 37 | * corresponding camera2 enums, not the camera1 strings. A few are defined here |
| 38 | * if they don't cleanly map to camera2 values. |
| 39 | */ |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 40 | struct Parameters { |
Eino-Ville Talvala | da6665c | 2012-08-29 17:37:16 -0700 | [diff] [blame] | 41 | /** |
| 42 | * Parameters and other state |
| 43 | */ |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 44 | int cameraId; |
| 45 | int cameraFacing; |
| 46 | |
| 47 | int previewWidth, previewHeight; |
| 48 | int32_t previewFpsRange[2]; |
| 49 | int previewFps; // deprecated, here only for tracking changes |
| 50 | int previewFormat; |
| 51 | |
| 52 | int previewTransform; // set by CAMERA_CMD_SET_DISPLAY_ORIENTATION |
| 53 | |
| 54 | int pictureWidth, pictureHeight; |
| 55 | |
| 56 | int32_t jpegThumbSize[2]; |
Eino-Ville Talvala | c695b7c | 2013-01-04 12:05:56 -0800 | [diff] [blame] | 57 | uint8_t jpegQuality, jpegThumbQuality; |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 58 | int32_t jpegRotation; |
| 59 | |
| 60 | bool gpsEnabled; |
| 61 | double gpsCoordinates[3]; |
| 62 | int64_t gpsTimestamp; |
| 63 | String8 gpsProcessingMethod; |
| 64 | |
| 65 | uint8_t wbMode; |
| 66 | uint8_t effectMode; |
| 67 | uint8_t antibandingMode; |
| 68 | uint8_t sceneMode; |
| 69 | |
| 70 | enum flashMode_t { |
| 71 | FLASH_MODE_OFF = 0, |
| 72 | FLASH_MODE_AUTO, |
| 73 | FLASH_MODE_ON, |
| 74 | FLASH_MODE_TORCH, |
Igor Murashkin | d32b99b | 2012-11-27 16:25:46 -0800 | [diff] [blame] | 75 | FLASH_MODE_RED_EYE = ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE, |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 76 | FLASH_MODE_INVALID = -1 |
| 77 | } flashMode; |
| 78 | |
| 79 | enum focusMode_t { |
Igor Murashkin | d32b99b | 2012-11-27 16:25:46 -0800 | [diff] [blame] | 80 | FOCUS_MODE_AUTO = ANDROID_CONTROL_AF_MODE_AUTO, |
| 81 | FOCUS_MODE_MACRO = ANDROID_CONTROL_AF_MODE_MACRO, |
| 82 | FOCUS_MODE_CONTINUOUS_VIDEO = ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO, |
| 83 | FOCUS_MODE_CONTINUOUS_PICTURE = ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE, |
| 84 | FOCUS_MODE_EDOF = ANDROID_CONTROL_AF_MODE_EDOF, |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 85 | FOCUS_MODE_INFINITY, |
| 86 | FOCUS_MODE_FIXED, |
| 87 | FOCUS_MODE_INVALID = -1 |
| 88 | } focusMode; |
| 89 | |
Eino-Ville Talvala | d6cc4a6 | 2012-10-16 10:17:30 -0700 | [diff] [blame] | 90 | uint8_t focusState; // Latest focus state from HAL |
| 91 | |
Eino-Ville Talvala | 95069fe | 2012-10-04 00:56:40 -0700 | [diff] [blame] | 92 | // For use with triggerAfWithAuto quirk |
| 93 | focusMode_t shadowFocusMode; |
| 94 | |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 95 | struct Area { |
| 96 | int left, top, right, bottom; |
| 97 | int weight; |
| 98 | Area() {} |
| 99 | Area(int left, int top, int right, int bottom, int weight): |
| 100 | left(left), top(top), right(right), bottom(bottom), |
| 101 | weight(weight) {} |
Eino-Ville Talvala | ac0cd56 | 2012-10-16 12:58:21 -0700 | [diff] [blame] | 102 | bool isEmpty() const { |
| 103 | return (left == 0) && (top == 0) && (right == 0) && (bottom == 0); |
| 104 | } |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 105 | }; |
| 106 | Vector<Area> focusingAreas; |
| 107 | |
| 108 | int32_t exposureCompensation; |
| 109 | bool autoExposureLock; |
| 110 | bool autoWhiteBalanceLock; |
| 111 | |
| 112 | Vector<Area> meteringAreas; |
| 113 | |
| 114 | int zoom; |
| 115 | |
| 116 | int videoWidth, videoHeight; |
| 117 | |
| 118 | bool recordingHint; |
| 119 | bool videoStabilization; |
| 120 | |
James Painter | e538206 | 2012-09-05 18:02:32 -0700 | [diff] [blame] | 121 | enum lightFxMode_t { |
| 122 | LIGHTFX_NONE = 0, |
| 123 | LIGHTFX_LOWLIGHT, |
| 124 | LIGHTFX_HDR |
| 125 | } lightFx; |
| 126 | |
Eino-Ville Talvala | 8a42dd8 | 2012-10-02 13:30:04 -0700 | [diff] [blame] | 127 | CameraParameters params; |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 128 | String8 paramsFlattened; |
| 129 | |
| 130 | // These parameters are also part of the camera API-visible state, but not |
| 131 | // directly listed in Camera.Parameters |
| 132 | bool storeMetadataInBuffers; |
| 133 | bool playShutterSound; |
| 134 | bool enableFaceDetect; |
| 135 | |
| 136 | bool enableFocusMoveMessages; |
| 137 | int afTriggerCounter; |
| 138 | int currentAfTriggerId; |
| 139 | bool afInMotion; |
| 140 | |
Eino-Ville Talvala | da6665c | 2012-08-29 17:37:16 -0700 | [diff] [blame] | 141 | int precaptureTriggerCounter; |
| 142 | |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 143 | uint32_t previewCallbackFlags; |
| 144 | bool previewCallbackOneShot; |
| 145 | |
Eino-Ville Talvala | da6665c | 2012-08-29 17:37:16 -0700 | [diff] [blame] | 146 | bool zslMode; |
| 147 | |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 148 | // Overall camera state |
| 149 | enum State { |
| 150 | DISCONNECTED, |
| 151 | STOPPED, |
| 152 | WAITING_FOR_PREVIEW_WINDOW, |
| 153 | PREVIEW, |
| 154 | RECORD, |
| 155 | STILL_CAPTURE, |
| 156 | VIDEO_SNAPSHOT |
| 157 | } state; |
| 158 | |
| 159 | // Number of zoom steps to simulate |
Eino-Ville Talvala | 161fac8 | 2012-09-06 18:08:16 -0700 | [diff] [blame] | 160 | static const unsigned int NUM_ZOOM_STEPS = 30; |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 161 | |
| 162 | // Full static camera info, object owned by someone else, such as |
| 163 | // Camera2Device. |
| 164 | const CameraMetadata *info; |
| 165 | |
| 166 | // Fast-access static device information; this is a subset of the |
| 167 | // information available through the staticInfo() method, used for |
| 168 | // frequently-accessed values or values that have to be calculated from the |
| 169 | // static information. |
| 170 | struct DeviceInfo { |
| 171 | int32_t arrayWidth; |
| 172 | int32_t arrayHeight; |
| 173 | uint8_t bestFaceDetectMode; |
| 174 | int32_t maxFaces; |
Eino-Ville Talvala | 8a42dd8 | 2012-10-02 13:30:04 -0700 | [diff] [blame] | 175 | struct OverrideModes { |
| 176 | flashMode_t flashMode; |
| 177 | uint8_t wbMode; |
| 178 | focusMode_t focusMode; |
| 179 | OverrideModes(): |
| 180 | flashMode(FLASH_MODE_INVALID), |
Igor Murashkin | d32b99b | 2012-11-27 16:25:46 -0800 | [diff] [blame] | 181 | wbMode(ANDROID_CONTROL_AWB_MODE_OFF), |
Eino-Ville Talvala | 8a42dd8 | 2012-10-02 13:30:04 -0700 | [diff] [blame] | 182 | focusMode(FOCUS_MODE_INVALID) { |
| 183 | } |
| 184 | }; |
| 185 | DefaultKeyedVector<uint8_t, OverrideModes> sceneModeOverrides; |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 186 | } fastInfo; |
| 187 | |
Eino-Ville Talvala | 8a42dd8 | 2012-10-02 13:30:04 -0700 | [diff] [blame] | 188 | // Quirks information; these are short-lived flags to enable workarounds for |
| 189 | // incomplete HAL implementations |
| 190 | struct Quirks { |
| 191 | bool triggerAfWithAuto; |
| 192 | bool useZslFormat; |
Igor Murashkin | 7373cbe | 2012-09-28 15:30:03 -0700 | [diff] [blame] | 193 | bool meteringCropRegion; |
Eino-Ville Talvala | 8a42dd8 | 2012-10-02 13:30:04 -0700 | [diff] [blame] | 194 | } quirks; |
| 195 | |
Eino-Ville Talvala | da6665c | 2012-08-29 17:37:16 -0700 | [diff] [blame] | 196 | /** |
| 197 | * Parameter manipulation and setup methods |
| 198 | */ |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 199 | |
| 200 | Parameters(int cameraId, int cameraFacing); |
| 201 | ~Parameters(); |
| 202 | |
| 203 | // Sets up default parameters |
| 204 | status_t initialize(const CameraMetadata *info); |
| 205 | |
Eino-Ville Talvala | e382ee2 | 2012-10-02 18:14:49 -0700 | [diff] [blame] | 206 | // Build fast-access device static info from static info |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 207 | status_t buildFastInfo(); |
Eino-Ville Talvala | e382ee2 | 2012-10-02 18:14:49 -0700 | [diff] [blame] | 208 | // Query for quirks from static info |
| 209 | status_t buildQuirks(); |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 210 | |
| 211 | // Get entry from camera static characteristics information. min/maxCount |
| 212 | // are used for error checking the number of values in the entry. 0 for |
| 213 | // max/minCount means to do no bounds check in that direction. In case of |
| 214 | // error, the entry data pointer is null and the count is 0. |
| 215 | camera_metadata_ro_entry_t staticInfo(uint32_t tag, |
| 216 | size_t minCount=0, size_t maxCount=0) const; |
| 217 | |
| 218 | // Validate and update camera parameters based on new settings |
Eino-Ville Talvala | 8a42dd8 | 2012-10-02 13:30:04 -0700 | [diff] [blame] | 219 | status_t set(const String8 ¶mString); |
| 220 | |
| 221 | // Retrieve the current settings |
| 222 | String8 get() const; |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 223 | |
Eino-Ville Talvala | da6665c | 2012-08-29 17:37:16 -0700 | [diff] [blame] | 224 | // Update passed-in request for common parameters |
| 225 | status_t updateRequest(CameraMetadata *request) const; |
| 226 | |
Eino-Ville Talvala | ec77108 | 2012-10-04 13:21:08 -0700 | [diff] [blame] | 227 | // Add/update JPEG entries in metadata |
| 228 | status_t updateRequestJpeg(CameraMetadata *request) const; |
| 229 | |
Igor Murashkin | 018d228 | 2012-09-18 18:23:49 -0700 | [diff] [blame] | 230 | // Calculate the crop region rectangle based on current stream sizes |
| 231 | struct CropRegion { |
| 232 | float left; |
| 233 | float top; |
| 234 | float width; |
| 235 | float height; |
Igor Murashkin | 7373cbe | 2012-09-28 15:30:03 -0700 | [diff] [blame] | 236 | |
| 237 | enum Outputs { |
| 238 | OUTPUT_PREVIEW = 0x01, |
| 239 | OUTPUT_VIDEO = 0x02, |
| 240 | OUTPUT_JPEG_THUMBNAIL = 0x04, |
| 241 | OUTPUT_PICTURE = 0x08, |
| 242 | }; |
Igor Murashkin | 018d228 | 2012-09-18 18:23:49 -0700 | [diff] [blame] | 243 | }; |
Igor Murashkin | 7373cbe | 2012-09-28 15:30:03 -0700 | [diff] [blame] | 244 | CropRegion calculateCropRegion(CropRegion::Outputs outputs) const; |
Igor Murashkin | 018d228 | 2012-09-18 18:23:49 -0700 | [diff] [blame] | 245 | |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 246 | // Static methods for debugging and converting between camera1 and camera2 |
| 247 | // parameters |
| 248 | |
| 249 | static const char *getStateName(State state); |
| 250 | |
| 251 | static int formatStringToEnum(const char *format); |
| 252 | static const char *formatEnumToString(int format); |
| 253 | |
| 254 | static int wbModeStringToEnum(const char *wbMode); |
Eino-Ville Talvala | 8a42dd8 | 2012-10-02 13:30:04 -0700 | [diff] [blame] | 255 | static const char* wbModeEnumToString(uint8_t wbMode); |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 256 | static int effectModeStringToEnum(const char *effectMode); |
| 257 | static int abModeStringToEnum(const char *abMode); |
| 258 | static int sceneModeStringToEnum(const char *sceneMode); |
| 259 | static flashMode_t flashModeStringToEnum(const char *flashMode); |
Eino-Ville Talvala | 8a42dd8 | 2012-10-02 13:30:04 -0700 | [diff] [blame] | 260 | static const char* flashModeEnumToString(flashMode_t flashMode); |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 261 | static focusMode_t focusModeStringToEnum(const char *focusMode); |
Eino-Ville Talvala | 8a42dd8 | 2012-10-02 13:30:04 -0700 | [diff] [blame] | 262 | static const char* focusModeEnumToString(focusMode_t focusMode); |
James Painter | e538206 | 2012-09-05 18:02:32 -0700 | [diff] [blame] | 263 | static lightFxMode_t lightFxStringToEnum(const char *lightFxMode); |
Eino-Ville Talvala | 1aecbcb | 2012-10-02 18:04:23 -0700 | [diff] [blame] | 264 | |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 265 | static status_t parseAreas(const char *areasCStr, |
| 266 | Vector<Area> *areas); |
Igor Murashkin | 7d2a4aa | 2012-10-05 17:09:09 -0700 | [diff] [blame] | 267 | |
| 268 | enum AreaKind |
| 269 | { |
| 270 | AREA_KIND_FOCUS, |
| 271 | AREA_KIND_METERING |
| 272 | }; |
| 273 | status_t validateAreas(const Vector<Area> &areas, |
| 274 | size_t maxRegions, |
| 275 | AreaKind areaKind) const; |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 276 | static bool boolFromString(const char *boolStr); |
| 277 | |
| 278 | // Map from camera orientation + facing to gralloc transform enum |
| 279 | static int degToTransform(int degrees, bool mirror); |
| 280 | |
Eino-Ville Talvala | d0cec0c | 2012-09-27 18:08:20 -0700 | [diff] [blame] | 281 | // API specifies FPS ranges are done in fixed point integer, with LSB = 0.001. |
| 282 | // Note that this doesn't apply to the (deprecated) single FPS value. |
Eino-Ville Talvala | c9d7e4d | 2012-09-27 14:18:13 -0700 | [diff] [blame] | 283 | static const int kFpsToApiScale = 1000; |
| 284 | |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 285 | // Transform between (-1000,-1000)-(1000,1000) normalized coords from camera |
| 286 | // API and HAL2 (0,0)-(activePixelArray.width/height) coordinates |
| 287 | int arrayXToNormalized(int width) const; |
| 288 | int arrayYToNormalized(int height) const; |
| 289 | int normalizedXToArray(int x) const; |
| 290 | int normalizedYToArray(int y) const; |
Igor Murashkin | af3d288 | 2012-10-04 14:22:18 -0700 | [diff] [blame] | 291 | |
| 292 | struct Range { |
| 293 | int min; |
| 294 | int max; |
| 295 | }; |
| 296 | |
| 297 | int32_t fpsFromRange(int32_t min, int32_t max) const; |
| 298 | |
Igor Murashkin | 7373cbe | 2012-09-28 15:30:03 -0700 | [diff] [blame] | 299 | private: |
| 300 | |
| 301 | // Convert between HAL2 sensor array coordinates and |
| 302 | // viewfinder crop-region relative array coordinates |
| 303 | int cropXToArray(int x) const; |
| 304 | int cropYToArray(int y) const; |
| 305 | int arrayXToCrop(int x) const; |
| 306 | int arrayYToCrop(int y) const; |
| 307 | |
| 308 | // Convert between viewfinder crop-region relative array coordinates |
| 309 | // and camera API (-1000,1000)-(1000,1000) normalized coords |
| 310 | int cropXToNormalized(int x) const; |
| 311 | int cropYToNormalized(int y) const; |
| 312 | int normalizedXToCrop(int x) const; |
| 313 | int normalizedYToCrop(int y) const; |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 314 | }; |
| 315 | |
| 316 | // This class encapsulates the Parameters class so that it can only be accessed |
| 317 | // by constructing a Lock object, which locks the SharedParameter's mutex. |
| 318 | class SharedParameters { |
| 319 | public: |
| 320 | SharedParameters(int cameraId, int cameraFacing): |
| 321 | mParameters(cameraId, cameraFacing) { |
| 322 | } |
| 323 | |
| 324 | template<typename S, typename P> |
| 325 | class BaseLock { |
| 326 | public: |
| 327 | BaseLock(S &p): |
| 328 | mParameters(p.mParameters), |
| 329 | mSharedParameters(p) { |
| 330 | mSharedParameters.mLock.lock(); |
| 331 | } |
| 332 | |
| 333 | ~BaseLock() { |
| 334 | mSharedParameters.mLock.unlock(); |
| 335 | } |
| 336 | P &mParameters; |
| 337 | private: |
| 338 | // Disallow copying, default construction |
| 339 | BaseLock(); |
| 340 | BaseLock(const BaseLock &); |
| 341 | BaseLock &operator=(const BaseLock &); |
| 342 | S &mSharedParameters; |
| 343 | }; |
| 344 | typedef BaseLock<SharedParameters, Parameters> Lock; |
| 345 | typedef BaseLock<const SharedParameters, const Parameters> ReadLock; |
| 346 | |
| 347 | // Access static info, read-only and immutable, so no lock needed |
| 348 | camera_metadata_ro_entry_t staticInfo(uint32_t tag, |
| 349 | size_t minCount=0, size_t maxCount=0) const { |
| 350 | return mParameters.staticInfo(tag, minCount, maxCount); |
| 351 | } |
| 352 | |
| 353 | // Only use for dumping or other debugging |
| 354 | const Parameters &unsafeAccess() { |
| 355 | return mParameters; |
| 356 | } |
| 357 | private: |
| 358 | Parameters mParameters; |
| 359 | mutable Mutex mLock; |
| 360 | }; |
| 361 | |
| 362 | |
| 363 | }; // namespace camera2 |
| 364 | }; // namespace android |
| 365 | |
| 366 | #endif |