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