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> |
| 26 | |
| 27 | #include "CameraMetadata.h" |
| 28 | |
| 29 | namespace android { |
| 30 | namespace camera2 { |
| 31 | |
Eino-Ville Talvala | 69230df | 2012-08-29 17:37:16 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Current camera state; this is the full state of the Camera under the old |
| 34 | * camera API (contents of the CameraParameters object in a more-efficient |
| 35 | * format, plus other state). The enum values are mostly based off the |
| 36 | * corresponding camera2 enums, not the camera1 strings. A few are defined here |
| 37 | * if they don't cleanly map to camera2 values. |
| 38 | */ |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 39 | struct Parameters { |
Eino-Ville Talvala | 69230df | 2012-08-29 17:37:16 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Parameters and other state |
| 42 | */ |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 43 | int cameraId; |
| 44 | int cameraFacing; |
| 45 | |
| 46 | int previewWidth, previewHeight; |
| 47 | int32_t previewFpsRange[2]; |
| 48 | int previewFps; // deprecated, here only for tracking changes |
| 49 | int previewFormat; |
| 50 | |
| 51 | int previewTransform; // set by CAMERA_CMD_SET_DISPLAY_ORIENTATION |
| 52 | |
| 53 | int pictureWidth, pictureHeight; |
| 54 | |
| 55 | int32_t jpegThumbSize[2]; |
| 56 | int32_t jpegQuality, jpegThumbQuality; |
| 57 | int32_t jpegRotation; |
| 58 | |
| 59 | bool gpsEnabled; |
| 60 | double gpsCoordinates[3]; |
| 61 | int64_t gpsTimestamp; |
| 62 | String8 gpsProcessingMethod; |
| 63 | |
| 64 | uint8_t wbMode; |
| 65 | uint8_t effectMode; |
| 66 | uint8_t antibandingMode; |
| 67 | uint8_t sceneMode; |
| 68 | |
| 69 | enum flashMode_t { |
| 70 | FLASH_MODE_OFF = 0, |
| 71 | FLASH_MODE_AUTO, |
| 72 | FLASH_MODE_ON, |
| 73 | FLASH_MODE_TORCH, |
| 74 | FLASH_MODE_RED_EYE = ANDROID_CONTROL_AE_ON_AUTO_FLASH_REDEYE, |
| 75 | FLASH_MODE_INVALID = -1 |
| 76 | } flashMode; |
| 77 | |
| 78 | enum focusMode_t { |
| 79 | FOCUS_MODE_AUTO = ANDROID_CONTROL_AF_AUTO, |
| 80 | FOCUS_MODE_MACRO = ANDROID_CONTROL_AF_MACRO, |
| 81 | FOCUS_MODE_CONTINUOUS_VIDEO = ANDROID_CONTROL_AF_CONTINUOUS_VIDEO, |
| 82 | FOCUS_MODE_CONTINUOUS_PICTURE = ANDROID_CONTROL_AF_CONTINUOUS_PICTURE, |
| 83 | FOCUS_MODE_EDOF = ANDROID_CONTROL_AF_EDOF, |
| 84 | FOCUS_MODE_INFINITY, |
| 85 | FOCUS_MODE_FIXED, |
| 86 | FOCUS_MODE_INVALID = -1 |
| 87 | } focusMode; |
| 88 | |
| 89 | struct Area { |
| 90 | int left, top, right, bottom; |
| 91 | int weight; |
| 92 | Area() {} |
| 93 | Area(int left, int top, int right, int bottom, int weight): |
| 94 | left(left), top(top), right(right), bottom(bottom), |
| 95 | weight(weight) {} |
| 96 | }; |
| 97 | Vector<Area> focusingAreas; |
| 98 | |
| 99 | int32_t exposureCompensation; |
| 100 | bool autoExposureLock; |
| 101 | bool autoWhiteBalanceLock; |
| 102 | |
| 103 | Vector<Area> meteringAreas; |
| 104 | |
| 105 | int zoom; |
| 106 | |
| 107 | int videoWidth, videoHeight; |
| 108 | |
| 109 | bool recordingHint; |
| 110 | bool videoStabilization; |
| 111 | |
James Painter | c3dbf1a | 2012-09-05 18:02:32 -0700 | [diff] [blame] | 112 | enum lightFxMode_t { |
| 113 | LIGHTFX_NONE = 0, |
| 114 | LIGHTFX_LOWLIGHT, |
| 115 | LIGHTFX_HDR |
| 116 | } lightFx; |
| 117 | |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 118 | String8 paramsFlattened; |
| 119 | |
| 120 | // These parameters are also part of the camera API-visible state, but not |
| 121 | // directly listed in Camera.Parameters |
| 122 | bool storeMetadataInBuffers; |
| 123 | bool playShutterSound; |
| 124 | bool enableFaceDetect; |
| 125 | |
| 126 | bool enableFocusMoveMessages; |
| 127 | int afTriggerCounter; |
| 128 | int currentAfTriggerId; |
| 129 | bool afInMotion; |
| 130 | |
Eino-Ville Talvala | 69230df | 2012-08-29 17:37:16 -0700 | [diff] [blame] | 131 | int precaptureTriggerCounter; |
| 132 | |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 133 | uint32_t previewCallbackFlags; |
| 134 | bool previewCallbackOneShot; |
| 135 | |
Eino-Ville Talvala | 69230df | 2012-08-29 17:37:16 -0700 | [diff] [blame] | 136 | bool zslMode; |
| 137 | |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 138 | // Overall camera state |
| 139 | enum State { |
| 140 | DISCONNECTED, |
| 141 | STOPPED, |
| 142 | WAITING_FOR_PREVIEW_WINDOW, |
| 143 | PREVIEW, |
| 144 | RECORD, |
| 145 | STILL_CAPTURE, |
| 146 | VIDEO_SNAPSHOT |
| 147 | } state; |
| 148 | |
| 149 | // Number of zoom steps to simulate |
Eino-Ville Talvala | 1540532 | 2012-09-06 18:08:16 -0700 | [diff] [blame] | 150 | static const unsigned int NUM_ZOOM_STEPS = 30; |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 151 | |
| 152 | // Full static camera info, object owned by someone else, such as |
| 153 | // Camera2Device. |
| 154 | const CameraMetadata *info; |
| 155 | |
| 156 | // Fast-access static device information; this is a subset of the |
| 157 | // information available through the staticInfo() method, used for |
| 158 | // frequently-accessed values or values that have to be calculated from the |
| 159 | // static information. |
| 160 | struct DeviceInfo { |
| 161 | int32_t arrayWidth; |
| 162 | int32_t arrayHeight; |
| 163 | uint8_t bestFaceDetectMode; |
| 164 | int32_t maxFaces; |
| 165 | } fastInfo; |
| 166 | |
Eino-Ville Talvala | 69230df | 2012-08-29 17:37:16 -0700 | [diff] [blame] | 167 | /** |
| 168 | * Parameter manipulation and setup methods |
| 169 | */ |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 170 | |
| 171 | Parameters(int cameraId, int cameraFacing); |
| 172 | ~Parameters(); |
| 173 | |
| 174 | // Sets up default parameters |
| 175 | status_t initialize(const CameraMetadata *info); |
| 176 | |
| 177 | // Build fast device info |
| 178 | status_t buildFastInfo(); |
| 179 | |
| 180 | // Get entry from camera static characteristics information. min/maxCount |
| 181 | // are used for error checking the number of values in the entry. 0 for |
| 182 | // max/minCount means to do no bounds check in that direction. In case of |
| 183 | // error, the entry data pointer is null and the count is 0. |
| 184 | camera_metadata_ro_entry_t staticInfo(uint32_t tag, |
| 185 | size_t minCount=0, size_t maxCount=0) const; |
| 186 | |
| 187 | // Validate and update camera parameters based on new settings |
| 188 | status_t set(const String8 ¶ms); |
| 189 | |
Eino-Ville Talvala | 69230df | 2012-08-29 17:37:16 -0700 | [diff] [blame] | 190 | // Update passed-in request for common parameters |
| 191 | status_t updateRequest(CameraMetadata *request) const; |
| 192 | |
Igor Murashkin | e500bc2 | 2012-09-18 18:23:49 -0700 | [diff] [blame] | 193 | // Calculate the crop region rectangle based on current stream sizes |
| 194 | struct CropRegion { |
| 195 | float left; |
| 196 | float top; |
| 197 | float width; |
| 198 | float height; |
| 199 | }; |
| 200 | CropRegion calculateCropRegion(void) const; |
| 201 | |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 202 | // Static methods for debugging and converting between camera1 and camera2 |
| 203 | // parameters |
| 204 | |
| 205 | static const char *getStateName(State state); |
| 206 | |
| 207 | static int formatStringToEnum(const char *format); |
| 208 | static const char *formatEnumToString(int format); |
| 209 | |
| 210 | static int wbModeStringToEnum(const char *wbMode); |
| 211 | static int effectModeStringToEnum(const char *effectMode); |
| 212 | static int abModeStringToEnum(const char *abMode); |
| 213 | static int sceneModeStringToEnum(const char *sceneMode); |
| 214 | static flashMode_t flashModeStringToEnum(const char *flashMode); |
| 215 | static focusMode_t focusModeStringToEnum(const char *focusMode); |
James Painter | c3dbf1a | 2012-09-05 18:02:32 -0700 | [diff] [blame] | 216 | static lightFxMode_t lightFxStringToEnum(const char *lightFxMode); |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 217 | static status_t parseAreas(const char *areasCStr, |
| 218 | Vector<Area> *areas); |
| 219 | static status_t validateAreas(const Vector<Area> &areas, |
| 220 | size_t maxRegions); |
| 221 | static bool boolFromString(const char *boolStr); |
| 222 | |
| 223 | // Map from camera orientation + facing to gralloc transform enum |
| 224 | static int degToTransform(int degrees, bool mirror); |
| 225 | |
Eino-Ville Talvala | b5d9113 | 2012-09-27 14:18:13 -0700 | [diff] [blame^] | 226 | static const int kFpsToApiScale = 1000; |
| 227 | |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 228 | // Transform between (-1000,-1000)-(1000,1000) normalized coords from camera |
| 229 | // API and HAL2 (0,0)-(activePixelArray.width/height) coordinates |
| 230 | int arrayXToNormalized(int width) const; |
| 231 | int arrayYToNormalized(int height) const; |
| 232 | int normalizedXToArray(int x) const; |
| 233 | int normalizedYToArray(int y) const; |
| 234 | |
| 235 | }; |
| 236 | |
| 237 | // This class encapsulates the Parameters class so that it can only be accessed |
| 238 | // by constructing a Lock object, which locks the SharedParameter's mutex. |
| 239 | class SharedParameters { |
| 240 | public: |
| 241 | SharedParameters(int cameraId, int cameraFacing): |
| 242 | mParameters(cameraId, cameraFacing) { |
| 243 | } |
| 244 | |
| 245 | template<typename S, typename P> |
| 246 | class BaseLock { |
| 247 | public: |
| 248 | BaseLock(S &p): |
| 249 | mParameters(p.mParameters), |
| 250 | mSharedParameters(p) { |
| 251 | mSharedParameters.mLock.lock(); |
| 252 | } |
| 253 | |
| 254 | ~BaseLock() { |
| 255 | mSharedParameters.mLock.unlock(); |
| 256 | } |
| 257 | P &mParameters; |
| 258 | private: |
| 259 | // Disallow copying, default construction |
| 260 | BaseLock(); |
| 261 | BaseLock(const BaseLock &); |
| 262 | BaseLock &operator=(const BaseLock &); |
| 263 | S &mSharedParameters; |
| 264 | }; |
| 265 | typedef BaseLock<SharedParameters, Parameters> Lock; |
| 266 | typedef BaseLock<const SharedParameters, const Parameters> ReadLock; |
| 267 | |
| 268 | // Access static info, read-only and immutable, so no lock needed |
| 269 | camera_metadata_ro_entry_t staticInfo(uint32_t tag, |
| 270 | size_t minCount=0, size_t maxCount=0) const { |
| 271 | return mParameters.staticInfo(tag, minCount, maxCount); |
| 272 | } |
| 273 | |
| 274 | // Only use for dumping or other debugging |
| 275 | const Parameters &unsafeAccess() { |
| 276 | return mParameters; |
| 277 | } |
| 278 | private: |
| 279 | Parameters mParameters; |
| 280 | mutable Mutex mLock; |
| 281 | }; |
| 282 | |
| 283 | |
| 284 | }; // namespace camera2 |
| 285 | }; // namespace android |
| 286 | |
| 287 | #endif |